summaryrefslogtreecommitdiff
path: root/examples/pgn.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pgn.py')
-rw-r--r--examples/pgn.py66
1 files changed, 42 insertions, 24 deletions
diff --git a/examples/pgn.py b/examples/pgn.py
index fc19c98..d9889d6 100644
--- a/examples/pgn.py
+++ b/examples/pgn.py
@@ -10,7 +10,18 @@
# Copyright 2004, by Alberto Santini http://www.albertosantini.it/chess/
#
from pyparsing import alphanums, nums, quotedString
-from pyparsing import Combine, Forward, Group, Literal, oneOf, OneOrMore, Optional, Suppress, ZeroOrMore, Word
+from pyparsing import (
+ Combine,
+ Forward,
+ Group,
+ Literal,
+ oneOf,
+ OneOrMore,
+ Optional,
+ Suppress,
+ ZeroOrMore,
+ Word,
+)
from pyparsing import ParseException
#
@@ -30,14 +41,14 @@ castle_queenside = oneOf("O-O-O 0-0-0 o-o-o")
castle_kingside = oneOf("O-O 0-0 o-o")
move_number = Optional(comment) + Word(nums) + dot
-m1 = file_coord + rank_coord # pawn move e.g. d4
-m2 = file_coord + capture + file_coord + rank_coord # pawn capture move e.g. dxe5
-m3 = file_coord + "8" + promote + piece # pawn promotion e.g. e8=Q
-m4 = piece + file_coord + rank_coord # piece move e.g. Be6
-m5 = piece + file_coord + file_coord + rank_coord # piece move e.g. Nbd2
-m6 = piece + rank_coord + file_coord + rank_coord # piece move e.g. R4a7
-m7 = piece + capture + file_coord + rank_coord # piece capture move e.g. Bxh7
-m8 = castle_queenside | castle_kingside # castling e.g. o-o
+m1 = file_coord + rank_coord # pawn move e.g. d4
+m2 = file_coord + capture + file_coord + rank_coord # pawn capture move e.g. dxe5
+m3 = file_coord + "8" + promote + piece # pawn promotion e.g. e8=Q
+m4 = piece + file_coord + rank_coord # piece move e.g. Be6
+m5 = piece + file_coord + file_coord + rank_coord # piece move e.g. Nbd2
+m6 = piece + rank_coord + file_coord + rank_coord # piece move e.g. R4a7
+m7 = piece + capture + file_coord + rank_coord # piece capture move e.g. Bxh7
+m8 = castle_queenside | castle_kingside # castling e.g. o-o
check = oneOf("+ ++")
mate = Literal("#")
@@ -46,8 +57,11 @@ nag = " $" + Word(nums)
decoration = check | mate | annotation | nag
variant = Forward()
-half_move = Combine((m3 | m1 | m2 | m4 | m5 | m6 | m7 | m8) + Optional(decoration)) \
- + Optional(comment) +Optional(variant)
+half_move = (
+ Combine((m3 | m1 | m2 | m4 | m5 | m6 | m7 | m8) + Optional(decoration))
+ + Optional(comment)
+ + Optional(variant)
+)
move = Suppress(move_number) + half_move + Optional(half_move)
variant << "(" + OneOrMore(move) + ")"
# grouping the plies (half-moves) for each move: useful to group annotations, variants...
@@ -56,19 +70,23 @@ move = Group(Suppress(move_number) + half_move + Optional(half_move))
variant << Group("(" + OneOrMore(move) + ")")
game_terminator = oneOf("1-0 0-1 1/2-1/2 *")
-pgnGrammar = Suppress(ZeroOrMore(tag)) + ZeroOrMore(move) + Optional(Suppress(game_terminator))
+pgnGrammar = (
+ Suppress(ZeroOrMore(tag)) + ZeroOrMore(move) + Optional(Suppress(game_terminator))
+)
+
+
+def parsePGN(pgn, bnf=pgnGrammar, fn=None):
+ try:
+ return bnf.parseString(pgn)
+ except ParseException as err:
+ print(err.line)
+ print(" " * (err.column - 1) + "^")
+ print(err)
-def parsePGN( pgn, bnf=pgnGrammar, fn=None ):
- try:
- return bnf.parseString( pgn )
- except ParseException as err:
- print(err.line)
- print(" "*(err.column-1) + "^")
- print(err)
if __name__ == "__main__":
- # input string
- pgn = """
+ # input string
+ pgn = """
[Event "ICC 5 0 u"]
[Site "Internet Chess Club"]
[Date "2004.01.25"]
@@ -89,6 +107,6 @@ Bg4 8. Nbd2 c5 9. h3 Be6 10. O-O-O Nc6 11. g4 Bd6 12. g5 Nd7 13. Rg1 d4 14.
g6 fxg6 15. Bg5 Rf8 16. a3 Bd5 17. Re1+ Nde5 18. Nxe5 Nxe5 19. Bf4 Rf5 20.
Bxe5 Rxe5 21. Rg5 Rxe1# {Black wins} 0-1
"""
- # parse input string
- tokens = parsePGN(pgn, pgnGrammar)
- print(tokens.dump())
+ # parse input string
+ tokens = parsePGN(pgn, pgnGrammar)
+ print(tokens.dump())