1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 00:12:29 +03:00

Values can be quoted.

This commit is contained in:
Rob Rix 2015-09-18 15:04:04 -04:00
parent fbcd667a4c
commit c46c35157a
2 changed files with 23 additions and 4 deletions

View File

@ -4,10 +4,21 @@ enum Swift: Equatable {
struct Parsers { struct Parsers {
static let alphabetic = ^"abcdefghijklmnopqrstuvwxyz".characters static let alphabetic = ^"abcdefghijklmnopqrstuvwxyz".characters
static let word = { $0.joinWithSeparator("") } <^> alphabetic*
static let ws = ^" \t\n".characters static let ws = ^" \t\n".characters
static let keyValue = KeyValue <^> (word <* ^"=" <*> word)
static let word = { $0.joinWithSeparator("") } <^> alphabetic+
static let quoted = join(^"'", join((concat <^> not(^"'")*), ^"'"))
static let keyValue = KeyValue <^> (word <* ^"=" <*> (quoted <|> word))
static let branch = Branch <^> (^"(" *> ws* *> word <*> sexpr* <* ws* <* ^")") static let branch = Branch <^> (^"(" *> ws* *> word <*> sexpr* <* ws* <* ^")")
static let sexpr: String -> State<Swift>? = never <* ws* static let sexpr: String -> State<Swift>? = never <* ws*
} }
} }
private func join(a: String -> State<String>?, _ b: String -> State<String>?) -> String -> State<String>? {
return (+) <^> (a <*> b)
}
private func concat(strings: [String]) -> String {
return strings.joinWithSeparator("")
}

View File

@ -1,6 +1,14 @@
final class SwiftTests: XCTestCase { final class SwiftTests: XCTestCase {
func testKeyValueAcceptsAlphabeticKeysAndValues() { func testValuesCanBeAlphabetic() {
XCTAssertEqual(Swift.Parsers.keyValue("key=value")?.value, .KeyValue("key", "value")) XCTAssertEqual(full(Swift.Parsers.keyValue)("key=value"), .KeyValue("key", "value"))
}
func testKeyValueCanBeQuoted() {
XCTAssertEqual(full(Swift.Parsers.keyValue)("key='value'"), .KeyValue("key", "'value'"))
}
func testQuotedMatchesQuotedStrings() {
XCTAssertEqual(full (Swift.Parsers.quoted)("'value'"), "'value'")
} }
} }