JavaScript Highligher: object literal parsing fixes
This commit is contained in:
parent
f27fc8eb9e
commit
5b9f59ec09
|
@ -119,9 +119,11 @@ class JavaScriptHighlighter {
|
||||||
} else {
|
} else {
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
consumeWhitespace()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeWhitespace(newlines: Bool = false) {
|
private func consumeWhitespace(newlines: Bool = true) {
|
||||||
let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces
|
let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces
|
||||||
while let char = peek(), charSet.contains(char) {
|
while let char = peek(), charSet.contains(char) {
|
||||||
consume()
|
consume()
|
||||||
|
@ -234,13 +236,17 @@ class JavaScriptHighlighter {
|
||||||
consumeObjectKey()
|
consumeObjectKey()
|
||||||
if peek() == ":" {
|
if peek() == ":" {
|
||||||
consume() // :
|
consume() // :
|
||||||
|
emit(token: .punctuation, range: prevCharRange())
|
||||||
|
consumeWhitespace()
|
||||||
while currentIndex < text.endIndex && peek() != "," && peek() != "}" {
|
while currentIndex < text.endIndex && peek() != "," && peek() != "}" {
|
||||||
consumeExpression()
|
consumeExpression()
|
||||||
}
|
}
|
||||||
if currentIndex < text.endIndex {
|
|
||||||
break object
|
|
||||||
}
|
}
|
||||||
} else if peek() == "," {
|
consumeWhitespace()
|
||||||
|
|
||||||
|
if peek() == "," {
|
||||||
|
consume() // ,
|
||||||
|
emit(token: .punctuation, range: prevCharRange())
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
|
@ -255,6 +261,7 @@ class JavaScriptHighlighter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeObjectKey() {
|
private func consumeObjectKey() {
|
||||||
|
consumeWhitespace()
|
||||||
guard let char = peek() else { return }
|
guard let char = peek() else { return }
|
||||||
let keyStart = currentIndex!
|
let keyStart = currentIndex!
|
||||||
if identifierStarts.contains(char) {
|
if identifierStarts.contains(char) {
|
||||||
|
@ -263,6 +270,7 @@ class JavaScriptHighlighter {
|
||||||
consumeString()
|
consumeString()
|
||||||
}
|
}
|
||||||
print("Object key: '\(text[keyStart..<currentIndex])'")
|
print("Object key: '\(text[keyStart..<currentIndex])'")
|
||||||
|
consumeWhitespace()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeDotLookup() {
|
private func consumeDotLookup() {
|
||||||
|
|
|
@ -8,6 +8,6 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
let source = "`+`${{a:3}}`"
|
let source = "{a: 1234 , 'b': 'blah', foo}"
|
||||||
_ = JavaScriptHighlighter(text: source).highlight()
|
_ = JavaScriptHighlighter(text: source).highlight()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue