-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
291 lines (256 loc) · 7.47 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Package chess provides functionality for reading and parsing chess games in PGN
(Portable Game Notation) format. It includes a scanner for reading multiple games
from a single source and a tokenizer for converting PGN text into processable tokens.
The scanner handles PGN-specific syntax including game metadata, moves, comments,
and variations. It supports streaming processing of large PGN files and provides
proper handling of game boundaries and special notation.
Example usage:
// Create scanner for PGN input
scanner := NewScanner(reader)
// Read all games
for scanner.HasNext() {
game, err := scanner.ScanGame()
if err != nil {
log.Fatal(err)
}
// Process game
}
// Tokenize a specific game
tokens, err := TokenizeGame(game)
*/
package chess
import (
"bufio"
"bytes"
"io"
)
// GameScanned represents a complete chess game in PGN format.
type GameScanned struct {
// Raw contains the complete PGN text of the game
Raw string
}
// TokenizeGame converts a PGN game into a sequence of tokens.
// Returns nil if the game is nil. Returns an error if tokenization fails.
//
// The function handles all PGN elements including moves, comments,
// annotations, and metadata tags.
//
// Example:
//
// tokens, err := TokenizeGame(game)
// if err != nil {
// // Handle error
// }
func TokenizeGame(game *GameScanned) ([]Token, error) {
if game == nil {
return nil, nil
}
lexer := NewLexer(game.Raw)
var tokens []Token
for {
token := lexer.NextToken()
if token.Type == EOF {
break
}
tokens = append(tokens, token)
}
return tokens, nil
}
// Scanner provides functionality to read chess games from a PGN source.
// It supports streaming processing of multiple games and proper handling
// of PGN syntax.
type Scanner struct {
scanner *bufio.Scanner
nextGame *GameScanned // Buffer for peeked game
lastError error // Store last error
}
// NewScanner creates a new PGN scanner that reads from the provided reader.
// The scanner is configured to properly split PGN games and handle
// PGN-specific syntax.
//
// Example:
//
// scanner := NewScanner(strings.NewReader(pgnText))
func NewScanner(r io.Reader) *Scanner {
s := bufio.NewScanner(r)
s.Split(splitPGNGames)
return &Scanner{scanner: s}
}
// ScanGame reads and returns the next game from the source.
// Returns nil and io.EOF when no more games are available.
// Returns nil and an error if reading fails.
//
// Example:
//
// game, err := scanner.ScanGame()
// if err == io.EOF {
// // No more games
// }
func (s *Scanner) ScanGame() (*GameScanned, error) {
// If we have a buffered game from HasNext(), return it
if s.nextGame != nil {
game := s.nextGame
s.nextGame = nil
return game, nil
}
// Otherwise scan the next game
if s.scanner.Scan() {
return &GameScanned{Raw: s.scanner.Text()}, nil
}
// Check for errors
if err := s.scanner.Err(); err != nil {
return nil, err
}
return nil, io.EOF
}
// HasNext returns true if there are more games available to read.
// This method can be used to iterate over all games in the source.
//
// Example:
//
// for scanner.HasNext() {
// game, err := scanner.ScanGame()
// // Process game
// }
func (s *Scanner) HasNext() bool {
// If we already have a buffered game, return true
if s.nextGame != nil {
return true
}
// Try to scan the next game
if s.scanner.Scan() {
// Store the game in the buffer
s.nextGame = &GameScanned{Raw: s.scanner.Text()}
return true
}
// Store any error that occurred
s.lastError = s.scanner.Err()
return false
}
// Split function for bufio.Scanner to split PGN games.
func splitPGNGames(data []byte, atEOF bool) (int, []byte, error) {
// Skip leading whitespace
start := skipLeadingWhitespace(data)
if start == len(data) {
return handleEOF(data, atEOF)
}
// Find the start of the game
start = findGameStart(data, start, atEOF)
if start == -1 {
return 0, nil, nil
}
// Process the game content
return processGameContent(data, start, atEOF)
}
// Helper to skip leading whitespace.
func skipLeadingWhitespace(data []byte) int {
start := 0
for ; start < len(data); start++ {
if !isWhitespace(data[start]) {
break
}
}
return start
}
// Helper to handle EOF scenarios.
func handleEOF(data []byte, atEOF bool) (int, []byte, error) {
if atEOF {
return len(data), nil, nil
}
return 0, nil, nil
}
// Helper to find the start of a game (first '[' character).
func findGameStart(data []byte, start int, atEOF bool) int {
// If the first character is not '[', find the next '[' character
if start < len(data) && data[start] != '[' {
idx := bytes.IndexByte(data[start:], '[')
if idx == -1 {
if atEOF {
return -1 // this could be removed as we return -1 in the next line anyway (just to be explicit and debuggable)
}
return -1
}
start += idx
}
return start
}
// Helper to process the content of a game and return the token or advance position.
func processGameContent(data []byte, start int, atEOF bool) (int, []byte, error) {
var i int // Loop variable
var inBrackets, inComment, foundResult bool // State variables
resultStart := -1 // Start position of result token
// Process the game content
for i = start; i < len(data); i++ {
// first check if we are in brackets or comments
inBrackets = updateBracketState(data[i], inBrackets, inComment)
inComment = updateCommentState(data[i], inComment)
// when we are not in brackets or comments, we can check for the result token
if foundResult && !inBrackets && !inComment && data[i] == '\n' {
nextGame := findNextGameStart(data[i:])
if nextGame != -1 {
// return the next game start position and the current game content
return i + nextGame, bytes.TrimSpace(data[start:i]), nil
}
}
// check for result token if we are not in brackets or comments and haven't found it yet
if !inBrackets && !inComment && !foundResult {
foundResult, resultStart = checkForResult(data, i)
}
}
// check for result token at EOF if we haven't found it yet
if atEOF && foundResult && resultStart > 0 {
return len(data), bytes.TrimSpace(data[start:]), nil
}
if !atEOF || i <= start {
return 0, nil, nil
}
// return the current game content
return len(data), bytes.TrimSpace(data[start:]), nil
}
// Helper to update bracket state based on current character.
func updateBracketState(ch byte, inBrackets bool, inComment bool) bool {
if ch == '[' && !inComment {
return true
} else if ch == ']' && !inComment {
return false
}
return inBrackets
}
// Helper to update comment state based on current character.
func updateCommentState(ch byte, inComment bool) bool {
if ch == '{' {
return true
} else if ch == '}' && inComment {
return false
}
return inComment
}
// Helper to find the next game start after a newline character.
func findNextGameStart(data []byte) int {
nextGame := bytes.Index(data, []byte("[Event "))
if nextGame != -1 {
return nextGame
}
return -1
}
// Helper to check for game result tokens (e.g., "1-0", "0-1", "1/2-1/2", "*").
func checkForResult(data []byte, i int) (bool, int) {
const minLength = 3 // Minimum length for results like "1-0"
const fullResultLength = 7 // Length for "1/2-1/2"
if len(data)-i >= minLength {
switch {
case bytes.HasPrefix(data[i:], []byte("1-0")):
return true, i
case bytes.HasPrefix(data[i:], []byte("0-1")):
return true, i
case len(data)-i >= fullResultLength && bytes.HasPrefix(data[i:], []byte("1/2-1/2")):
return true, i
case data[i] == '*':
return true, i
default:
break
}
}
return false, -1
}