Skip to content

Commit

Permalink
Rename parser regex structs to Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
pdil committed Aug 3, 2024
1 parent d82ebf2 commit d837861
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Sources/ChessKit/Parsers/EngineLANParser+Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//

extension EngineLANParser {

/// Contains useful regex strings for engine LAN parsing.
struct Regex {
static let full = #"^([a-h][1-8]){2}[qrbn]?$"#
struct Pattern {
static let move = #"^([a-h][1-8]){2}[qrbn]?$"#
}

}
4 changes: 2 additions & 2 deletions Sources/ChessKit/Parsers/EngineLANParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public enum EngineLANParser {
}

// MARK: - Private

/// Returns whether the provided engine LAN is valid.
///
/// - parameter lan: The LAN string to check.
/// - returns: Whether the LAN is valid.
///
private static func isValid(lan: String) -> Bool {
lan.range(of: EngineLANParser.Regex.full, options: .regularExpression) != nil
lan.range(of: EngineLANParser.Pattern.move, options: .regularExpression) != nil
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Parsers/PGNParser+Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

extension PGNParser {

/// Contains useful regex strings for PGN parsing.
struct Pattern {
// tag pair components
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Parsers/PGNParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public enum PGNParser {
}

// movetext

for element in game.moves.pgnRepresentation {
switch element {
case .whiteNumber(let number):
Expand Down
2 changes: 1 addition & 1 deletion Sources/ChessKit/Parsers/SANParser+Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extension SANParser {

/// Contains useful regex strings for SAN parsing.
struct Regex {
struct Pattern {
static let full = #"(([Oo0]-[Oo0](-[Oo0])?|[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](\=[QRBN])?[+#]?))"#

// piece kinds
Expand Down
104 changes: 52 additions & 52 deletions Sources/ChessKit/Parsers/SANParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// Parses and converts the Standard Algebraic Notation (SAN)
/// of a chess move.
public enum SANParser {

/// Parses a SAN string and returns a move.
///
/// - parameter san: The SAN string of a move.
Expand All @@ -21,25 +21,25 @@ public enum SANParser {
in position: Position
) -> Move? {
guard isValid(san: san) else { return nil }

let color = position.sideToMove
var checkstate = Move.CheckState.none

if san.contains("#") {
checkstate = .checkmate
} else if san.contains("+") {
checkstate = .check
}

// castling
var castling: Castling?
if san.range(of: Regex.shortCastle, options: .regularExpression) != nil {

if san.range(of: Pattern.shortCastle, options: .regularExpression) != nil {
castling = Castling(side: .king, color: color)
} else if san.range(of: Regex.longCastle, options: .regularExpression) != nil {
} else if san.range(of: Pattern.longCastle, options: .regularExpression) != nil {
castling = Castling(side: .queen, color: color)
}

if let castling {
return Move(
result: .castle(castling),
Expand All @@ -49,11 +49,11 @@ public enum SANParser {
checkState: checkstate
)
}

// pawns
if let range = san.range(of: Regex.pawnFile, options: .regularExpression), let end = targetSquare(for: san) {
if let range = san.range(of: Pattern.pawnFile, options: .regularExpression), let end = targetSquare(for: san) {
let startingFile = String(san[range])

let board = Board(position: position)
let possiblePiece = position.pieces
.filter {
Expand All @@ -63,36 +63,36 @@ public enum SANParser {
board.canMove(pieceAt: $0.square, to: end)
}
.first

guard var pawn = possiblePiece else {
return nil
}

let start = pawn.square
pawn.square = end

var move: Move

if isCapture(san: san), let capturedPiece = position.piece(at: end) {
move = Move(result: .capture(capturedPiece), piece: pawn, start: start, end: capturedPiece.square, checkState: checkstate)
} else {
move = Move(result: .move, piece: pawn, start: start, end: end, checkState: checkstate)
}

if let promotionPieceKind = promotionPiece(for: san) {
move.promotedPiece = Piece(promotionPieceKind, color: color, square: end)
}

return move
}

// pieces
if let range = san.range(of: Regex.pieceKind, options: .regularExpression) {
if let range = san.range(of: Pattern.pieceKind, options: .regularExpression) {
if let pieceKind = Piece.Kind(rawValue: String(san[range])),
let end = targetSquare(for: san) {
var move: Move?
let disambiguation = self.disambiguation(for: san)

let board = Board(position: position)
let possiblePiece = position.pieces
.filter { $0.kind == pieceKind && $0.color == color }
Expand All @@ -112,29 +112,29 @@ public enum SANParser {
}
}
.first

guard var piece = possiblePiece else {
return nil
}

let start = piece.square
piece.square = end

if isCapture(san: san), let capturedPiece = position.piece(at: end) {
move = Move(result: .capture(capturedPiece), piece: piece, start: start, end: end, checkState: checkstate)
} else {
move = Move(result: .move, piece: piece, start: start, end: end, checkState: checkstate)
}

move?.disambiguation = disambiguation

return move
}
}

return nil
}

/// Converts a ``Move`` object into a SAN string.
///
/// - parameter move: The chess move to convert.
Expand All @@ -146,62 +146,62 @@ public enum SANParser {
return "\(castling.side.notation)\(move.checkState.notation)"
default:
var pieceNotation = move.piece.kind.notation

if move.piece.kind == .pawn, case .capture = move.result {
pieceNotation = move.start.file.rawValue
}

var disambiguationNotation = ""

if let disambiguation = move.disambiguation {
switch disambiguation {
case let .byFile(file): disambiguationNotation = file.rawValue
case let .byRank(rank): disambiguationNotation = "\(rank.value)"
case let .bySquare(square): disambiguationNotation = square.notation
}
}

var captureNotation = ""

if case .capture = move.result {
captureNotation = "x"
}

var promotionNotation = ""

if let promotedPiece = move.promotedPiece {
promotionNotation = "=\(promotedPiece.kind.notation)"
}

return "\(pieceNotation)\(disambiguationNotation)\(captureNotation)\(move.end.notation)\(promotionNotation)\(move.checkState.notation)"
}
}

// MARK: - Private

/// Returns whether the provided SAN is valid.
///
/// - parameter san: The SAN string to check.
/// - returns: Whether the SAN is valid.
///
private static func isValid(san: String) -> Bool {
san.range(of: SANParser.Regex.full, options: .regularExpression) != nil
san.range(of: SANParser.Pattern.full, options: .regularExpression) != nil
}

/// Returns the target square for a SAN move.
///
/// - parameter san: The SAN represenation of a move.
/// - returns: The square the move is targeting, or `nil`
/// if the SAN is invalid.
///
private static func targetSquare(for san: String) -> Square? {
if let range = san.range(of: Regex.targetSquare, options: .regularExpression) {
if let range = san.range(of: Pattern.targetSquare, options: .regularExpression) {
return Square(String(san[range]))
} else {
return nil
}
}

/// Checks if a SAN string contains a capture.
///
/// - parameter san: The SAN represenation of a move.
Expand All @@ -210,23 +210,23 @@ public enum SANParser {
private static func isCapture(san: String) -> Bool {
san.contains("x")
}

/// Checks if a SAN string contains a promotion.
///
/// - parameter san: The SAN represenation of a move.
/// - returns: The kind of piece that is being promoted to,
/// or `nil` if the SAN does not contain a promotion.
///
private static func promotionPiece(for san: String) -> Piece.Kind? {
guard let range = san.range(of: Regex.promotion, options: .regularExpression) else {
guard let range = san.range(of: Pattern.promotion, options: .regularExpression) else {
return nil
}

return Piece.Kind(
rawValue: san[range].replacingOccurrences(of: "=", with: "")
)
}

/// Checks if a SAN string contains a disambiguation.
///
/// - parameter san: The SAN represenation of a move.
Expand All @@ -237,21 +237,21 @@ public enum SANParser {
/// square, the SAN contains a disambiguating file, rank, or square
/// so the piece that is moving can be determined.
private static func disambiguation(for san: String) -> Move.Disambiguation? {
guard let range = san.range(of: Regex.disambiguation, options: .regularExpression) else {
guard let range = san.range(of: Pattern.disambiguation, options: .regularExpression) else {
return nil
}

let value = String(san[range])
if let rankRange = value.range(of: Regex.rank, options: .regularExpression), let rank = Int(String(value[rankRange])) {

if let rankRange = value.range(of: Pattern.rank, options: .regularExpression), let rank = Int(String(value[rankRange])) {
return .byRank(Square.Rank(rank))
} else if let fileRange = value.range(of: Regex.file, options: .regularExpression), let file = Square.File(rawValue: String(value[fileRange])) {
} else if let fileRange = value.range(of: Pattern.file, options: .regularExpression), let file = Square.File(rawValue: String(value[fileRange])) {
return .byFile(file)
} else if let squareRange = value.range(of: Regex.square, options: .regularExpression) {
} else if let squareRange = value.range(of: Pattern.square, options: .regularExpression) {
return .bySquare(Square(String(value[squareRange])))
} else {
return nil
}
}

}

0 comments on commit d837861

Please sign in to comment.