From 3c13f56c2009a9e103bd87d5416887f7260c3126 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sat, 3 Aug 2024 12:14:24 -0400 Subject: [PATCH] Improve parser tests --- .../ChessKit/Parsers/PGNParser+Regex.swift | 11 +-- Sources/ChessKit/Parsers/PGNParser.swift | 84 ++++++++----------- .../Parsers/EngineLANParserTests.swift | 28 +++++-- .../Parsers/PGNParserTests.swift | 36 +++----- .../PGNParserPerformanceTests.swift | 27 ++++++ .../ChessKitTests/Utilities/SampleGames.swift | 28 +++++++ 6 files changed, 132 insertions(+), 82 deletions(-) create mode 100644 Tests/ChessKitTests/Performance/PGNParserPerformanceTests.swift create mode 100644 Tests/ChessKitTests/Utilities/SampleGames.swift diff --git a/Sources/ChessKit/Parsers/PGNParser+Regex.swift b/Sources/ChessKit/Parsers/PGNParser+Regex.swift index 9342439..becb0f3 100644 --- a/Sources/ChessKit/Parsers/PGNParser+Regex.swift +++ b/Sources/ChessKit/Parsers/PGNParser+Regex.swift @@ -6,21 +6,22 @@ extension PGNParser { /// Contains useful regex strings for PGN parsing. - struct Regex { + struct Pattern { // tag pair components static let tags = #"\[[^\]]+\]"# static let tagPair = #"\[([^"]+?)\s"([^"]+)"\]"# + // move text + static let moveText = #"\d{1,}\.{1,3}\s?(([Oo0]-[Oo0](-[Oo0])?|[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](\=[QRBN])?[+#]?)([\?!]{1,2})?(\s?\$\d)?(\s?\{.+?\})?(\s(1-0|0-1|1\/2-1\/2))?\s?){1,2}"# + static let moveNumber = #"^\d{1,}"# + static let singleMove = "(\(castle)?|\(move)?)(\\s?\(annotation))?(\\s?\(comment))?" + // move pair components static let castle = #"[Oo0]-[Oo0](-[Oo0])"# static let move = #"[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](\=[QRBN])?[+#]"# static let annotation = #"\$\d"# static let comment = #"\{.+?\}"# static let result = #"(1-0|0-1|1\/2-1\/2)"# - - static let full = #"\d{1,}\.{1,3}\s?(([Oo0]-[Oo0](-[Oo0])?|[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](\=[QRBN])?[+#]?)([\?!]{1,2})?(\s?\$\d)?(\s?\{.+?\})?(\s(1-0|0-1|1\/2-1\/2))?\s?){1,2}"# - - static let moveNumber = #"^\d{1,}"# } } diff --git a/Sources/ChessKit/Parsers/PGNParser.swift b/Sources/ChessKit/Parsers/PGNParser.swift index 6d8a458..2804cb2 100644 --- a/Sources/ChessKit/Parsers/PGNParser.swift +++ b/Sources/ChessKit/Parsers/PGNParser.swift @@ -5,15 +5,8 @@ import Foundation -/// Positional assessments. -// periphery:ignore -enum PositionAnnotation { - // -} - /// Parses and converts the Portable Game Notation (PGN) /// of a chess game. -/// public enum PGNParser { /// Contains the contents of a single parsed move pair. @@ -58,44 +51,43 @@ public enum PGNParser { // tags - let tags: [(String, String)]? = try? NSRegularExpression(pattern: Regex.tags) + let tags: [(String, String)]? = try? NSRegularExpression(pattern: Pattern.tags) .matches(in: processedPGN, range: range) .map { NSString(string: pgn).substring(with: $0.range) .trimmingCharacters(in: .whitespacesAndNewlines) } - .compactMap { tag in - let tagRange = NSRange(0..= 1, - matches[0].numberOfRanges >= 3 { - let key = matches[0].range(at: 1) - let value = matches[0].range(at: 2) - - return ( - NSString(string: tag).substring(with: key) - .trimmingCharacters(in: .whitespacesAndNewlines), - NSString(string: tag).substring(with: value) - .trimmingCharacters(in: .whitespacesAndNewlines) - ) - } else { - return nil + let tagRange = NSRange(0..= 1, + matches[0].numberOfRanges >= 3 { + let key = matches[0].range(at: 1) + let value = matches[0].range(at: 2) + + return ( + NSString(string: tag).substring(with: key) + .trimmingCharacters(in: .whitespacesAndNewlines), + NSString(string: tag).substring(with: value) + .trimmingCharacters(in: .whitespacesAndNewlines) + ) + } else { + return nil + } } - } let parsedTags = parsed(tags: Dictionary(tags ?? []) { a, _ in a }) // movetext - let moves: [String] + let moveText: [String] do { - moves = try NSRegularExpression(pattern: Regex.full) + moveText = try NSRegularExpression(pattern: Pattern.moveText) .matches(in: processedPGN, range: range) .map { NSString(string: pgn).substring(with: $0.range) @@ -105,18 +97,16 @@ public enum PGNParser { return nil } - let parsedMoves = moves.compactMap { move -> ParsedMove? in + let parsedMoves = moveText.compactMap { move -> ParsedMove? in let range = NSRange(0..= 1 && m.count <= 2 @@ -124,16 +114,16 @@ public enum PGNParser { return nil } - let whiteAnnotation = try? NSRegularExpression(pattern: Regex.annotation) + let whiteAnnotation = try? NSRegularExpression(pattern: Pattern.annotation) .matches(in: m[0], range: NSRange(0..