From ec2cf8570fbf8f27a5286949cb080fc8c598639b Mon Sep 17 00:00:00 2001 From: David Cairns Date: Wed, 4 Feb 2015 18:32:00 -0800 Subject: [PATCH 1/2] Adds tests for repetition operators on strings --- MadnessTests/ParserTests.swift | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/MadnessTests/ParserTests.swift b/MadnessTests/ParserTests.swift index 064956e..c8f5a99 100644 --- a/MadnessTests/ParserTests.swift +++ b/MadnessTests/ParserTests.swift @@ -193,7 +193,51 @@ final class ParserTests: XCTestCase { func testMToNRepetitionMatchesUpToN() { assertEqual(mToN("xxxx")?.1, "x") } - + + + // MARK: Repetition shorthand + let zeroOrMoreSimple = "x"* + + func testZeroOrMoreSimpleRepetitionAcceptsTheEmptyString() { + assertNotNil(zeroOrMoreSimple("")) + } + + func testZeroOrMoreSimpleRepetitionAcceptsUnmatchedStrings() { + assertNotNil(zeroOrMoreSimple("y")) + } + + func testZeroOrMoreSimpleRepetitionDoesNotAdvanceWithUnmatchedStrings() { + assertEqual(zeroOrMoreSimple("y")?.1, "y") + } + + func testZeroOrMoreSimpleRepetitionParsesUnmatchedStringsAsEmptyArrays() { + assertEqual(zeroOrMoreSimple("y")?.0, []) + } + + func testZeroOrMoreSimpleRepetitionParsesAMatchedString() { + assertEqual(zeroOrMoreSimple("x")?.0, ["x"]) + } + + func testZeroOrMoreSimpleRepetitionParsesMatchedStrings() { + assertEqual(zeroOrMoreSimple("xx")?.0, ["x", "x"]) + } + + + let oneOrMoreSimple = "x"+ + + func testOneOrMoreSimpleRepetitionRejectsTheEmptyString() { + assertNil(oneOrMoreSimple("")) + } + + func testOneOrMoreSimpleRepetitionParsesASingleMatchedString() { + assertEqual(oneOrMoreSimple("x")?.0, ["x"]) + } + + func testOneOrMoreSimpleRepetitonParsesMultipleMatchedStrings() { + assertEqual(oneOrMoreSimple("xxy")?.0, ["x", "x"]) + } + + // MARK: Ignoring From 7ac0c3ea50f1d74f2cc970572b401fd3ac0f9353 Mon Sep 17 00:00:00 2001 From: David Cairns Date: Wed, 4 Feb 2015 18:32:33 -0800 Subject: [PATCH 2/2] Adds sugar functions for e.g. `"x"+` and `"x"*` --- Madness/Parser.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Madness/Parser.swift b/Madness/Parser.swift index 70eedb3..7a075d4 100644 --- a/Madness/Parser.swift +++ b/Madness/Parser.swift @@ -102,6 +102,11 @@ public postfix func * (parser: Parser.Function) -> Parser<[T]>.Function { return repeat(parser) } +/// Creates a parser from `string`, and parses it 0 or more times. +public postfix func * (string: String) -> Parser<[String]>.Function { + return repeat(%(string)) +} + /// Parses `parser` 0 or more times and drops its parse trees. public postfix func * (parser: Parser<()>.Function) -> Parser<()>.Function { return repeat(parser) --> const(()) @@ -112,6 +117,11 @@ public postfix func + (parser: Parser.Function) -> Parser<[T]>.Function { return repeat(parser, 1.. Parser<[String]>.Function { + return repeat(%(string), 1...Function) -> Parser<()>.Function { return repeat(parser, 1.. const(())