Fix issues in Sources

Sources

sources
This commit is contained in:
David Jennes
2018-09-20 05:10:18 +02:00
parent 799490198f
commit 3f4622f54f
21 changed files with 346 additions and 350 deletions

View File

@@ -1,6 +1,5 @@
import Foundation
extension String {
/// Split a string by a separator leaving quoted phrases together
func smartSplit(separator: Character = " ") -> [String] {
@@ -10,37 +9,18 @@ extension String {
var singleQuoteCount = 0
var doubleQuoteCount = 0
let specialCharacters = ",|:"
func appendWord(_ word: String) {
if components.count > 0 {
if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) {
components[components.count-1] += word
} else if specialCharacters.contains(word) {
components[components.count-1] += word
} else if word != "(" && word.first == "(" || word != ")" && word.first == ")" {
components.append(String(word.prefix(1)))
appendWord(String(word.dropFirst()))
} else if word != "(" && word.last == "(" || word != ")" && word.last == ")" {
appendWord(String(word.dropLast()))
components.append(String(word.suffix(1)))
} else {
components.append(word)
}
} else {
components.append(word)
}
}
for character in self {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }
if character == "'" {
singleQuoteCount += 1
} else if character == "\"" {
doubleQuoteCount += 1
}
if character == separate {
if separate != separator {
word.append(separate)
} else if (singleQuoteCount % 2 == 0 || doubleQuoteCount % 2 == 0) && !word.isEmpty {
appendWord(word)
appendWord(word, to: &components)
word = ""
}
@@ -54,11 +34,33 @@ extension String {
}
if !word.isEmpty {
appendWord(word)
appendWord(word, to: &components)
}
return components
}
private func appendWord(_ word: String, to components: inout [String]) {
let specialCharacters = ",|:"
if !components.isEmpty {
if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) {
components[components.count - 1] += word
} else if specialCharacters.contains(word) {
components[components.count - 1] += word
} else if word != "(" && word.first == "(" || word != ")" && word.first == ")" {
components.append(String(word.prefix(1)))
appendWord(String(word.dropFirst()), to: &components)
} else if word != "(" && word.last == "(" || word != ")" && word.last == ")" {
appendWord(String(word.dropLast()), to: &components)
components.append(String(word.suffix(1)))
} else {
components.append(word)
}
} else {
components.append(word)
}
}
}
public struct SourceMap: Equatable {
@@ -72,7 +74,7 @@ public struct SourceMap: Equatable {
static let unknown = SourceMap()
public static func ==(lhs: SourceMap, rhs: SourceMap) -> Bool {
public static func == (lhs: SourceMap, rhs: SourceMap) -> Bool {
return lhs.filename == rhs.filename && lhs.location == rhs.location
}
}
@@ -88,20 +90,20 @@ public class Token: Equatable {
/// A token representing a template block.
case block
}
public let contents: String
public let kind: Kind
public let sourceMap: SourceMap
/// Returns the underlying value as an array seperated by spaces
public private(set) lazy var components: [String] = self.contents.smartSplit()
init(contents: String, kind: Kind, sourceMap: SourceMap) {
self.contents = contents
self.kind = kind
self.sourceMap = sourceMap
}
/// A token representing a piece of text.
public static func text(value: String, at sourceMap: SourceMap) -> Token {
return Token(contents: value, kind: .text, sourceMap: sourceMap)
@@ -121,9 +123,8 @@ public class Token: Equatable {
public static func block(value: String, at sourceMap: SourceMap) -> Token {
return Token(contents: value, kind: .block, sourceMap: sourceMap)
}
public static func == (lhs: Token, rhs: Token) -> Bool {
return lhs.contents == rhs.contents && lhs.kind == rhs.kind && lhs.sourceMap == rhs.sourceMap
}
}