Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 02242f6

Browse files
committed
Merge pull request #9 from Carthage/fix-quoted-nodes
Fix parsing quoted nodes
2 parents 7682301 + 6049efd commit 02242f6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

OGDL/Parser.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ internal postfix func |? <T>(parser: Parser<T>.Function) -> Parser<T?>.Function
4949
}
5050

5151
private let char_control = NSCharacterSet.controlCharacterSet()
52-
private let char_text = char_control.invertedSet - NSCharacterSet.whitespaceAndNewlineCharacterSet()
53-
private let char_word = char_text - ",()"
52+
private let char_text = char_control.invertedSet - NSCharacterSet.newlineCharacterSet()
53+
private let char_word = char_text - ",()" - NSCharacterSet.whitespaceCharacterSet()
5454
private let char_space = NSCharacterSet.whitespaceCharacterSet()
5555
private let char_break = NSCharacterSet.newlineCharacterSet()
5656

@@ -60,11 +60,15 @@ private let char_end = char_control - NSCharacterSet.whitespaceAndNewlineCharact
6060
private let wordStart: Parser<String>.Function = %(char_word - "#'\"")
6161
private let wordChars: Parser<String>.Function = (%(char_word - "'\""))* --> { strings in join("", strings) }
6262
private let word: Parser<String>.Function = wordStart ++ wordChars --> (+)
63-
private let string: Parser<String>.Function = (%char_text | %char_space)+ --> { strings in join("", strings) }
6463
private let br: Parser<()>.Function = ignore(%char_break)
6564
private let eof: Parser<()>.Function = { $0 == "" ? ((), "") : nil }
66-
private let comment: Parser<()>.Function = ignore(%"#" ++ string ++ (br | eof))
67-
private let quoted: Parser<String>.Function = (ignore(%"'") ++ string ++ ignore(%"'")) | (ignore(%"\"") ++ string ++ ignore(%"\""))
65+
private let comment: Parser<()>.Function = ignore(%"#" ++ (%char_text)+ ++ (br | eof))
66+
// TODO: Escape sequences.
67+
private let singleQuotedChars: Parser<String>.Function = (%(char_text - "'"))* --> { strings in join("", strings) }
68+
private let singleQuoted: Parser<String>.Function = ignore(%"'") ++ singleQuotedChars ++ ignore(%"'")
69+
private let doubleQuotedChars: Parser<String>.Function = (%(char_text - "\""))* --> { strings in join("", strings) }
70+
private let doubleQuoted: Parser<String>.Function = ignore(%"\"") ++ doubleQuotedChars ++ ignore(%"\"")
71+
private let quoted: Parser<String>.Function = singleQuoted | doubleQuoted
6872
private let requiredSpace: Parser<()>.Function = ignore((comment | %char_space)+)
6973
private let optionalSpace: Parser<()>.Function = ignore((comment | %char_space)*)
7074
private let separator: Parser<()>.Function = ignore(optionalSpace ++ %"," ++ optionalSpace)
@@ -125,7 +129,7 @@ private let block: Int -> Parser<()>.Function = { n in const(nil) }
125129
/// Parses a single descendent element.
126130
///
127131
/// This is an element which may be an in-line descendent, and which may further have in-line descendents of its own.
128-
private let descendent = (word | quoted) --> { Node(value: $0) }
132+
private let descendent = value --> { Node(value: $0) }
129133

130134
/// Parses a sequence of hierarchically descending elements, e.g.:
131135
///

OGDLTests/ParserSpec.swift

+13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ class ParserSpec: QuickSpec {
6161
expect(parsedGraph).to(equal(expectedGraph))
6262
}
6363

64+
it("should parse quoted nodes") {
65+
let expectedGraph = [
66+
Node(value: "foo", children: [
67+
Node(value: "bar", children: [
68+
Node(value: "fuzz buzz")
69+
])
70+
])
71+
]
72+
73+
let parsedGraph = parse(graph, "foo \"bar\" \"fuzz buzz\"")
74+
expect(parsedGraph).to(equal(expectedGraph))
75+
}
76+
6477
// TODO: Not yet supported. See Carthage/ogdl-swift#6.
6578
pending("should parse siblings") {
6679
let expectedGraph = [

0 commit comments

Comments
 (0)