Documentation
¶
Overview ¶
Package chessnote provides a high-performance, production-grade Go library for parsing Portable Game Notation (PGN), the universal standard for chess game data.
Package chessnote provides a high-performance, production-grade Go library for parsing Portable Game Notation (PGN), the universal standard for chess game data.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var PieceSymbols = map[rune]PieceType{ 'N': Knight, 'B': Bishop, 'R': Rook, 'Q': Queen, 'K': King, }
PieceSymbols maps a rune representation of a piece in PGN to a PieceType. Pawns are not represented by a symbol in PGN.
Functions ¶
func SplitMultiGame ¶ added in v0.1.1
SplitMultiGame takes a string containing multiple PGN games and splits them into a slice of individual game strings. It normalizes line endings to handle different file formats (e.g., Windows-style \r\n).
This utility is useful for pre-processing PGN files that contain an entire database of games before passing each individual game to the parser.
Types ¶
type Game ¶
type Game struct {
// Tags is a map of PGN tag key-value pairs.
Tags map[string]string
// Moves is a slice of moves made in the game.
Moves []Move
// Result is the final result of the game (e.g., "1-0", "0-1").
Result string
}
Game represents a single parsed PGN game, including its tag pairs, movetext, and final result.
func ParseString ¶
func ParseString(s string, opts ...ParserOption) (*Game, error)
ParseString is a convenience helper that parses a PGN string. It is intended for quickly parsing a complete PGN string already in memory. For parsing from files or network streams, creating a Parser with NewParser is recommended.
type Move ¶
type Move struct {
// From is the starting square of the move. For many moves, this may be
// partially or fully zero, as PGN format often omits this information
// when it's not needed for disambiguation.
From Square
// To is the destination square of the move. This is always specified.
To Square
// Piece is the type of piece that was moved.
Piece PieceType
// Promotion is the piece type a pawn is promoted to. It is zero
// (Pawn) if there is no promotion.
Promotion PieceType
// IsCapture indicates whether the move was a capture.
IsCapture bool
// IsCheck indicates whether the move resulted in a check.
IsCheck bool
// IsMate indicates whether the move resulted in a checkmate.
IsMate bool
// IsKingsideCastle indicates a kingside castling move (O-O).
IsKingsideCastle bool
// IsQueensideCastle indicates a queenside castling move (O-O-O).
IsQueensideCastle bool
// Variations lists any alternative move sequences that could have been
// played. This is used for representing Recursive Annotation Variations (RAVs).
Variations [][]Move
// NAGs is a slice of Numeric Annotation Glyphs (e.g., $1, $2)
// associated with the move.
NAGs []int
}
Move represents a single move made by one player, capturing all details expressed in Standard Algebraic Notation (SAN).
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a PGN parser that reads from an io.Reader and parses it into a Game. It implements a standard recursive descent parser.
type ParserConfig ¶ added in v0.1.3
type ParserConfig struct {
// Strict mode requires that a PGN game must end with a valid result token
// (*, 1-0, 0-1, or 1/2-1/2). When disabled (lax mode), a game can end
// at the end of the file without a result token.
// It is enabled by default.
Strict bool
}
ParserConfig holds configuration settings for the parser.
type ParserOption ¶ added in v0.1.3
type ParserOption func(*ParserConfig)
A ParserOption configures a Parser.
func WithLaxParsing ¶ added in v0.1.3
func WithLaxParsing() ParserOption
WithLaxParsing returns a ParserOption that disables strict parsing mode. In lax mode, the parser will not require a final game result token and will successfully parse a game that ends abruptly at the end of the file.
type PieceType ¶
type PieceType int
PieceType defines the type of chess piece.
const ( // Pawn represents a pawn piece. Note that this is the zero value for PieceType. Pawn PieceType = iota // Knight represents a knight piece. Knight // Bishop represents a bishop piece. Bishop // Rook represents a rook piece. Rook // Queen represents a queen piece. Queen // King represents a king piece. King )
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
advanced_iterator
command
This example demonstrates a more advanced use case: iterating through a game's entire move tree, including all variations (Recursive Annotation Variations).
|
This example demonstrates a more advanced use case: iterating through a game's entire move tree, including all variations (Recursive Annotation Variations). |
|
basic_parser
command
This example demonstrates how to parse a PGN file from your local disk.
|
This example demonstrates how to parse a PGN file from your local disk. |
|
multiple-game-pgn
command
|
|
|
parser_options
command
|
|
|
internal
|
|