Browse Source

JavaScript Highlighter: Fix numbers starting with dot

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

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

@@ -295,8 +295,19 @@ class JavaScriptHighlighter {
295 295
     
296 296
     private func consumeDotLookup() {
297 297
         consume() // .
298
-        print("Dot lookup")
299
-        emit(token: .punctuation, range: prevCharRange())
298
+        guard let char = peek() else { return }
299
+        if identifierStarts.contains(char) {
300
+            print("Dot lookup")
301
+            emit(token: .punctuation, range: prevCharRange())
302
+            consumeIdentifier()
303
+        } else if CharacterSet.decimalDigits.contains(char) {
304
+            let numberStart = text.index(before: currentIndex)
305
+            while let char = peek(), CharacterSet.decimalDigits.contains(char) {
306
+                consume()
307
+            }
308
+            print("Number: \(text[numberStart..<currentIndex])")
309
+            emit(token: .number, range: range(from: numberStart, to: currentIndex))
310
+        }
300 311
     }
301 312
     
302 313
     private func consumeArray() {

+ 1
- 1
jstest/main.swift View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 import Foundation
10 10
 
11
-let source = "`$foo ${blah} bar`"
11
+let source = ".1 + .2"
12 12
 let highlighter = JavaScriptHighlighter(text: source)
13 13
 highlighter.debug = true
14 14
 highlighter.highlight()

Loading…
Cancel
Save