Skip to content

Commit 23cc921

Browse files
committed
Added initial documentation for the parser and removed and unused struct for the game result.
1 parent 8d62342 commit 23cc921

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

PGNParser/DraughtsMove.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import Foundation
1010
import ParserCombinator
1111

12+
/**
13+
Describes a single move performed by one player.
14+
*/
1215
public struct DraughtsPieceMove {
1316

1417
let from: Int
@@ -19,6 +22,18 @@ public struct DraughtsPieceMove {
1922

2023
extension DraughtsPieceMove {
2124

25+
/**
26+
A constructor with the parameters in the order of draughts portable game notation.
27+
28+
This constructor simplifies the creation of a parser for the move.
29+
It should remain in an extension so that it does not replace the default constructor.
30+
31+
- parameter from: The location of the piece that is to be moved.
32+
33+
- parameter hasCapture: True if the piece captures another piece, false if none are captured.
34+
35+
- parameter to: The location at which the piece resides after the move.
36+
*/
2237
init(from: Int, hasCapture: Bool, to: Int) {
2338
self.from = from
2439
self.to = to
@@ -27,24 +42,30 @@ extension DraughtsPieceMove {
2742

2843
}
2944

45+
/**
46+
A description of the movement of pieces by both players.
47+
48+
Black is optional as it may not have moved in this round.
49+
A move in technical draughts terms refers to a round of moves by both players.
50+
*/
3051
public struct DraughtsMove {
3152

3253
let white: DraughtsPieceMove
3354
let black: DraughtsPieceMove?
3455

3556
}
3657

37-
public enum Winner {
38-
39-
case black
40-
case white
41-
case draw
42-
43-
}
44-
4558
extension DraughtsMove {
4659

47-
/** */
60+
/**
61+
Parse the input draughts portable game notation string into an array of draughts moves.
62+
63+
Can apply functions to values that are wrapped in a parser context.
64+
65+
- parameter fromPortableGameNotation: The pgn string describing the moves for a draughts game.
66+
67+
- returns: A result of the parse. If successful it will contain the move objects.
68+
*/
4869
public static func parse(fromPortableGameNotation notation: String) -> ParseResult<[DraughtsMove]> {
4970

5071
return DraughtsNotationParser.portableGameNotation().run(withInput: notation)

PGNParser/DraughtsNotationParser.swift

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
import ParserCombinator
1010
import Currier
1111

12-
/** */
12+
/**
13+
Creates a parser to parse a description a draughts game in portable game notation to DraughtsMove objects.
14+
*/
1315
struct DraughtsNotationParser {
1416

15-
/** A function to create a parser which handles the parseing of portable game notation for draughts. */
17+
/**
18+
Create a parser which performs the parseing of portable game notation for draughts.
19+
20+
Once created the parser can be called multiple times for multiple input game strings.
21+
22+
- returns: A parser which creates a list of draughts moves from a string in the correct type.
23+
*/
1624
static func portableGameNotation() -> Parser<[DraughtsMove]> {
1725

18-
let lowercaseXIsTrue = character(isEqualTo: "x").map() { _ in return true }
19-
let hyphenIsFalse = character(isEqualTo: "-").map() { _ in return false }
20-
let fullStop = character(isEqualTo: ".")
21-
22-
let numberWithPoint = whitespace.zeroOneOrMany *> digit.oneOrMany *> fullStop
23-
2426
let singlePieceMove = curry(DraughtsPieceMove.init) <^> integerNumber
2527
<*> lowercaseXIsTrue <|> hyphenIsFalse
2628
<*> integerNumber
@@ -30,7 +32,25 @@ struct DraughtsNotationParser {
3032
return moveRound.oneOrMany
3133
}
3234

33-
/** */
35+
/// A parser which resolves lowercase x to a boolean true and fails on all other input strings.
36+
private static let lowercaseXIsTrue = character(isEqualTo: "x").map() { _ in return true }
37+
38+
/// A parser which resolves a hyphen to a boolean false and fails on all other input strings.
39+
private static let hyphenIsFalse = character(isEqualTo: "-").map() { _ in return false }
40+
41+
/// A parser which resolves a full stop to a success and fails on all other input strings.
42+
private static let fullStop = character(isEqualTo: ".")
43+
44+
/// A parser which resolves numbers followed by a fullStop and fails on all other input strings.
45+
private static let numberWithPoint = whitespace.zeroOneOrMany *> digit.oneOrMany *> fullStop
46+
47+
/**
48+
Combine a description of a single player turn into a full move with up to two player turns.
49+
50+
- parameter turn: A parser for a single turn performed by one player.
51+
52+
- returns: A parser for a draughts move object describing both playes turn for a move.
53+
*/
3454
private static func twoPlayerTurn(_ turn: Parser<DraughtsPieceMove>) -> Parser<DraughtsMove> {
3555

3656
let singleMove = whitespace.zeroOneOrMany *> turn

0 commit comments

Comments
 (0)