Skip to content

Commit

Permalink
Merge pull request #984 from snowdrop4/visit-san
Browse files Browse the repository at this point in the history
Add `pgn.BaseVisitor.begin_parse_san()` method
  • Loading branch information
niklasf committed Apr 25, 2023
2 parents bd371f6 + 7d670a4 commit bb3a2a4
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ def end_headers(self) -> Optional[SkipType]:
"""Called after visiting game headers."""
pass

def begin_parse_san(self, board: chess.Board, san: str) -> Optional[SkipType]:
"""
When the visitor is used by a parser, this is called at the start of
each standard algebraic notation detailing a move.
"""
pass

def parse_san(self, board: chess.Board, san: str) -> chess.Move:
"""
When the visitor is used by a parser, this is called to parse a move
Expand Down Expand Up @@ -1682,14 +1689,15 @@ def read_game(handle: TextIO, *, Visitor: Any = GameBuilder) -> Any:
visitor.visit_result(token)
else:
# Parse SAN tokens.
try:
move = visitor.parse_san(board_stack[-1], token)
except ValueError as error:
visitor.handle_error(error)
skip_variation_depth = 1
else:
visitor.visit_move(board_stack[-1], move)
board_stack[-1].push(move)
if visitor.begin_parse_san(board_stack[-1], token) is not SKIP:
try:
move = visitor.parse_san(board_stack[-1], token)
except ValueError as error:
visitor.handle_error(error)
skip_variation_depth = 1
else:
visitor.visit_move(board_stack[-1], move)
board_stack[-1].push(move)
visitor.visit_board(board_stack[-1])

if fresh_line:
Expand Down

0 comments on commit bb3a2a4

Please sign in to comment.