-
Notifications
You must be signed in to change notification settings - Fork 14
Lexer
Corentin Giaufer Saubert edited this page Dec 27, 2024
·
1 revision
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.
// 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
}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", "!", "?"
)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
}
}// 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
}
}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
}
}- 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