Skip to content

Commit bb3a2a4

Browse files
authored
Merge pull request #984 from snowdrop4/visit-san
Add `pgn.BaseVisitor.begin_parse_san()` method
2 parents bd371f6 + 7d670a4 commit bb3a2a4

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

chess/pgn.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,13 @@ def end_headers(self) -> Optional[SkipType]:
10431043
"""Called after visiting game headers."""
10441044
pass
10451045

1046+
def begin_parse_san(self, board: chess.Board, san: str) -> Optional[SkipType]:
1047+
"""
1048+
When the visitor is used by a parser, this is called at the start of
1049+
each standard algebraic notation detailing a move.
1050+
"""
1051+
pass
1052+
10461053
def parse_san(self, board: chess.Board, san: str) -> chess.Move:
10471054
"""
10481055
When the visitor is used by a parser, this is called to parse a move
@@ -1682,14 +1689,15 @@ def read_game(handle: TextIO, *, Visitor: Any = GameBuilder) -> Any:
16821689
visitor.visit_result(token)
16831690
else:
16841691
# Parse SAN tokens.
1685-
try:
1686-
move = visitor.parse_san(board_stack[-1], token)
1687-
except ValueError as error:
1688-
visitor.handle_error(error)
1689-
skip_variation_depth = 1
1690-
else:
1691-
visitor.visit_move(board_stack[-1], move)
1692-
board_stack[-1].push(move)
1692+
if visitor.begin_parse_san(board_stack[-1], token) is not SKIP:
1693+
try:
1694+
move = visitor.parse_san(board_stack[-1], token)
1695+
except ValueError as error:
1696+
visitor.handle_error(error)
1697+
skip_variation_depth = 1
1698+
else:
1699+
visitor.visit_move(board_stack[-1], move)
1700+
board_stack[-1].push(move)
16931701
visitor.visit_board(board_stack[-1])
16941702

16951703
if fresh_line:

0 commit comments

Comments
 (0)