Skip to content

Commit b0a8e8a

Browse files
committed
Fix quote issue
1 parent 341b76c commit b0a8e8a

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Sources/ASTParser/parser.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,42 @@ private func parseASTString(astString: String) -> [Node] {
5757
var stack = Array<Node>()
5858
var data = ""
5959
var quoteStarted = false
60+
var quoteChar: Character? = nil
6061
var sources: [Node] = []
62+
6163
for char in astString.characters {
64+
6265
if char == "(" && !quoteStarted {
6366
let node = Node()
64-
if let chuzzledData = data.chuzzle(), lastNode = stack.last where chuzzledData.characters.count > 0 {
67+
if data.characters.count > 0, let lastNode = stack.last, let chuzzledData = data.chuzzle() {
6568
lastNode.contents = chuzzledData
6669
if lastNode.contents == "source_file" { sources += [lastNode] }
6770
}
6871
stack.append(node)
6972
data = ""
7073
} else if char == ")" && !quoteStarted {
71-
var poppedNode: Node?
72-
if stack.count > 0 {
73-
poppedNode = stack.removeLast()
74-
if let chuzzledData = data.chuzzle(), poppedNode = poppedNode where chuzzledData.characters.count > 0 {
74+
if case let poppedNode = stack.removeLast() where stack.count > 0 {
75+
if data.characters.count > 0, let chuzzledData = data.chuzzle() {
7576
poppedNode.contents = chuzzledData
7677
}
77-
data = ""
78-
}
79-
if let last = stack.last, let poppedNode = poppedNode {
80-
last.nodes += [poppedNode]
78+
stack.last!.nodes += [poppedNode]
79+
8180
}
81+
data = ""
8282
} else {
8383
data = data + String(char)
84-
if char == "\"" || char == "'" {
85-
quoteStarted = !quoteStarted
86-
}
84+
if data == "(source_file" { print("Yes") }
85+
if char == "\"" || char == "'" {
86+
if quoteChar == nil {
87+
quoteChar = char
88+
quoteStarted = true
89+
} else if char == quoteChar {
90+
quoteChar = nil
91+
quoteStarted = false
92+
}
93+
}
8794
}
95+
8896
}
8997
return sources
9098
}

0 commit comments

Comments
 (0)