From 5b9f59ec098fe31ed4fcd511067ae5c8610164d4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 3 Apr 2020 10:04:52 -0400 Subject: [PATCH] JavaScript Highligher: object literal parsing fixes --- .../JavaScriptHighlighter.swift | 18 +++++++++++++----- jstest/main.swift | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift index 6ccdbd4..f147c29 100644 --- a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift +++ b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift @@ -119,9 +119,11 @@ class JavaScriptHighlighter { } else { consume() } + + consumeWhitespace() } - private func consumeWhitespace(newlines: Bool = false) { + private func consumeWhitespace(newlines: Bool = true) { let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces while let char = peek(), charSet.contains(char) { consume() @@ -234,13 +236,17 @@ class JavaScriptHighlighter { consumeObjectKey() if peek() == ":" { consume() // : + emit(token: .punctuation, range: prevCharRange()) + consumeWhitespace() while currentIndex < text.endIndex && peek() != "," && peek() != "}" { consumeExpression() } - if currentIndex < text.endIndex { - break object - } - } else if peek() == "," { + } + consumeWhitespace() + + if peek() == "," { + consume() // , + emit(token: .punctuation, range: prevCharRange()) continue } else { break @@ -255,6 +261,7 @@ class JavaScriptHighlighter { } private func consumeObjectKey() { + consumeWhitespace() guard let char = peek() else { return } let keyStart = currentIndex! if identifierStarts.contains(char) { @@ -263,6 +270,7 @@ class JavaScriptHighlighter { consumeString() } print("Object key: '\(text[keyStart..