Skip to content
Corentin Giaufer Saubert edited this page Dec 27, 2024 · 1 revision

PGN Lexer Guide

Overview

The PGN lexer converts chess game notation into a stream of tokens for further processing. It handles standard PGN elements including moves, comments, annotations, and metadata.

Basic Usage

Creating a Lexer

// Initialize with PGN text
lexer := NewLexer("1. e4 e5 2. Nf3 {Good move}")

// Process tokens
for {
    token := lexer.NextToken()
    if token.Type == EOF {
        break
    }
    // Use token
}

Token Types

The lexer recognizes these main token types:

type TokenType int

const (
    NOTATION  // Chess moves: "e4", "Nf3", "O-O"
    COMMENT   // Comments: "{text}" or "; text"
    TAG       // Metadata: [Event "World Championship"]
    NUMBER    // Move numbers: "1.", "2."
    NAG      // Annotations: "$1", "!", "?"
)

Common Tasks

Processing a Game

lexer := NewLexer(`
[Event "World Championship"]
[Site "London"]

1. e4 {Popular opening} e5 2. Nf3
`)

for {
    token := lexer.NextToken()
    switch token.Type {
    case NOTATION:
        // Handle move
    case COMMENT:
        // Handle comment
    case TAG:
        // Handle metadata
    case EOF:
        return
    }
}

Reading Game Metadata

// Read PGN tags
lexer := NewLexer(`
[Event "World Championship"]
[Site "London"]
[Date "2024.01.01"]
`)

for {
    token := lexer.NextToken()
    if token.Type == TAG {
        // Process tag content
    }
    if token.Type == EOF {
        break
    }
}

Processing Moves and Comments

lexer := NewLexer("1. e4 {Strong move} e5 {Response}")

for {
    token := lexer.NextToken()
    switch token.Type {
    case NOTATION:
        fmt.Printf("Move: %s\n", token.Literal)
    case COMMENT:
        fmt.Printf("Comment: %s\n", token.Literal)
    case EOF:
        return
    }
}

Supported PGN Elements

  • Standard moves (e4, Nf3, O-O)
  • Comments ({text} or ; text)
  • Tags ([Key "Value"])
  • Move numbers (1., 2., ...)
  • Annotations (!, ?, $1)
  • Variations ((...))
  • Special moves (O-O, O-O-O)
  • Commands

Clone this wiki locally