Browse Source

JavaScript Highlighter code cleanup

master
Shadowfacts 3 months ago
parent
commit
f27fc8eb9e
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5
1 changed files with 47 additions and 12 deletions
  1. 47
    12
      MongoView/Synax Highlighting/JavaScriptHighlighter.swift

+ 47
- 12
MongoView/Synax Highlighting/JavaScriptHighlighter.swift View File

@@ -77,8 +77,25 @@ class JavaScriptHighlighter {
77 77
 
78 78
         return attributed
79 79
     }
80
+    
81
+    private func emit(token: TokenType, range: NSRange) {
82
+        let color: NSColor
83
+        switch token {
84
+        case .string:
85
+            color = .systemRed
86
+        case .number:
87
+            color = .systemBlue
88
+        case .punctuation:
89
+            color = .systemTeal
90
+        case .identifier:
91
+            return
92
+        }
93
+        attributed.addAttribute(.foregroundColor, value: color, range: range)
94
+    }
80 95
 
81 96
     private func consumeExpression() {
97
+        consumeWhitespace()
98
+        
82 99
         guard let char = peek() else { return }
83 100
         
84 101
         if identifierStarts.contains(char) {
@@ -103,6 +120,13 @@ class JavaScriptHighlighter {
103 120
             consume()
104 121
         }
105 122
     }
123
+    
124
+    private func consumeWhitespace(newlines: Bool = false) {
125
+        let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces
126
+        while let char = peek(), charSet.contains(char) {
127
+            consume()
128
+        }
129
+    }
106 130
 
107 131
     private func consumeIdentifier() {
108 132
         let identifierStart = currentIndex!
@@ -124,7 +148,7 @@ class JavaScriptHighlighter {
124 148
             }
125 149
         }
126 150
         print("Number: \(text[numberStart..<currentIndex])")
127
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemBlue, range: range(from: numberStart, to: currentIndex))
151
+        emit(token: .number, range: range(from: numberStart, to: currentIndex))
128 152
     }
129 153
     
130 154
     private func consumeString() {
@@ -137,7 +161,7 @@ class JavaScriptHighlighter {
137 161
             consume() // string closing quote
138 162
         }
139 163
         print("String: \(text[stringStart..<currentIndex])")
140
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringStart, to: currentIndex))
164
+        emit(token: .string, range: range(from: stringStart, to: currentIndex))
141 165
     }
142 166
     
143 167
     private func consumeTemplateString() {
@@ -148,7 +172,7 @@ class JavaScriptHighlighter {
148 172
                 consume() // $
149 173
                 consume() // {
150 174
                 print("Template string fragment: '\(text[stringFragmentStart!..<currentIndex])'")
151
-                attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringFragmentStart!, to: currentIndex))
175
+                emit(token: .string, range: range(from: stringFragmentStart!, to: currentIndex))
152 176
                 consumeTemplateStringExpression()
153 177
                 stringFragmentStart = currentIndex
154 178
                 if currentIndex < text.endIndex && peek() == "}" {
@@ -158,7 +182,7 @@ class JavaScriptHighlighter {
158 182
                 stringFragmentStart = stringFragmentStart ?? currentIndex
159 183
                 consume() // `
160 184
                 print("Template string fragment: '\(text[stringFragmentStart!..<currentIndex])'")
161
-                attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringFragmentStart!, to: currentIndex))
185
+                emit(token: .string, range: range(from: stringFragmentStart!, to: currentIndex))
162 186
                 stringFragmentStart = nil
163 187
                 break
164 188
             } else {
@@ -167,7 +191,7 @@ class JavaScriptHighlighter {
167 191
         }
168 192
         if let start = stringFragmentStart {
169 193
             print("Template string fragment: '\(text[start..<currentIndex])'")
170
-            attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: start, to: currentIndex))
194
+            emit(token: .string, range: range(from: start, to: currentIndex))
171 195
         }
172 196
     }
173 197
     
@@ -187,7 +211,7 @@ class JavaScriptHighlighter {
187 211
     private func consumeFunctionCallOrGrouping() {
188 212
         consume() // (
189 213
         print("Opening (")
190
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
214
+        emit(token: .punctuation, range: prevCharRange())
191 215
         indent += "  "
192 216
         while currentIndex < text.endIndex && peek() != ")" {
193 217
             consumeExpression()
@@ -196,14 +220,14 @@ class JavaScriptHighlighter {
196 220
         if currentIndex < text.endIndex {
197 221
             consume() // )
198 222
             print("Closing )")
199
-            attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
223
+            emit(token: .punctuation, range: prevCharRange())
200 224
         }
201 225
     }
202 226
     
203 227
     private func consumeObject() {
204 228
         consume() // {
205 229
         print("Opening {")
206
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
230
+        emit(token: .punctuation, range: prevCharRange())
207 231
         indent += "  "
208 232
         object:
209 233
         while currentIndex < text.endIndex && peek() != "}" {
@@ -226,7 +250,7 @@ class JavaScriptHighlighter {
226 250
         if currentIndex < text.endIndex {
227 251
             consume() // }
228 252
             print("Closing }")
229
-            attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
253
+            emit(token: .punctuation, range: prevCharRange())
230 254
         }
231 255
     }
232 256
     
@@ -244,13 +268,13 @@ class JavaScriptHighlighter {
244 268
     private func consumeDotLookup() {
245 269
         consume() // .
246 270
         print("Dot lookup")
247
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
271
+        emit(token: .punctuation, range: prevCharRange())
248 272
     }
249 273
     
250 274
     private func consumeArray() {
251 275
         consume() // [
252 276
         print("Opening [")
253
-        attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
277
+        emit(token: .punctuation, range: prevCharRange())
254 278
         indent += "  "
255 279
         array:
256 280
         while currentIndex < text.endIndex && peek() != "]" {
@@ -258,6 +282,8 @@ class JavaScriptHighlighter {
258 282
             while currentIndex < text.endIndex {
259 283
                 if peek() == "," {
260 284
                     consume() // ,
285
+                    print("Array separator")
286
+                    emit(token: .punctuation, range: prevCharRange())
261 287
                     break
262 288
                 } else if peek() == "]" {
263 289
                     break array
@@ -272,8 +298,17 @@ class JavaScriptHighlighter {
272 298
         if currentIndex < text.endIndex {
273 299
             consume() // ]
274 300
             print("Closing ]")
275
-            attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
301
+            emit(token: .punctuation, range: prevCharRange())
276 302
         }
277 303
     }
278 304
     
279 305
 }
306
+
307
+extension JavaScriptHighlighter {
308
+    enum TokenType {
309
+        case identifier
310
+        case punctuation
311
+        case number
312
+        case string
313
+    }
314
+}

Loading…
Cancel
Save