Browse Source

JavaScript Highligher: object literal parsing fixes

master
Shadowfacts 3 months ago
parent
commit
5b9f59ec09
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5
2 changed files with 14 additions and 6 deletions
  1. 13
    5
      MongoView/Synax Highlighting/JavaScriptHighlighter.swift
  2. 1
    1
      jstest/main.swift

+ 13
- 5
MongoView/Synax Highlighting/JavaScriptHighlighter.swift View File

@@ -119,9 +119,11 @@ class JavaScriptHighlighter {
119 119
         } else {
120 120
             consume()
121 121
         }
122
+        
123
+        consumeWhitespace()
122 124
     }
123 125
     
124
-    private func consumeWhitespace(newlines: Bool = false) {
126
+    private func consumeWhitespace(newlines: Bool = true) {
125 127
         let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces
126 128
         while let char = peek(), charSet.contains(char) {
127 129
             consume()
@@ -234,13 +236,17 @@ class JavaScriptHighlighter {
234 236
             consumeObjectKey()
235 237
             if peek() == ":" {
236 238
                 consume() // :
239
+                emit(token: .punctuation, range: prevCharRange())
240
+                consumeWhitespace()
237 241
                 while currentIndex < text.endIndex && peek() != "," && peek() != "}" {
238 242
                     consumeExpression()
239 243
                 }
240
-                if currentIndex < text.endIndex {
241
-                    break object
242
-                }
243
-            } else if peek() == "," {
244
+            }
245
+            consumeWhitespace()
246
+
247
+            if peek() == "," {
248
+                consume() // ,
249
+                emit(token: .punctuation, range: prevCharRange())
244 250
                 continue
245 251
             } else {
246 252
                 break
@@ -255,6 +261,7 @@ class JavaScriptHighlighter {
255 261
     }
256 262
     
257 263
     private func consumeObjectKey() {
264
+        consumeWhitespace()
258 265
         guard let char = peek() else { return }
259 266
         let keyStart = currentIndex!
260 267
         if identifierStarts.contains(char) {
@@ -263,6 +270,7 @@ class JavaScriptHighlighter {
263 270
             consumeString()
264 271
         }
265 272
         print("Object key: '\(text[keyStart..<currentIndex])'")
273
+        consumeWhitespace()
266 274
     }
267 275
     
268 276
     private func consumeDotLookup() {

+ 1
- 1
jstest/main.swift View File

@@ -8,6 +8,6 @@
8 8
 
9 9
 import Foundation
10 10
 
11
-let source = "`+`${{a:3}}`"
11
+let source = "{a: 1234 , 'b': 'blah', foo}"
12 12
 _ = JavaScriptHighlighter(text: source).highlight()
13 13
 

Loading…
Cancel
Save