elo-ratings 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/elo-ratings.gemspec +148 -0
- data/ext/bayeselo/.gitignore +4 -0
- data/ext/bayeselo/CBradleyTerry.cpp +670 -0
- data/ext/bayeselo/CBradleyTerry.h +137 -0
- data/ext/bayeselo/CCDistribution.cpp +135 -0
- data/ext/bayeselo/CCDistribution.h +44 -0
- data/ext/bayeselo/CCDistributionCUI.cpp +104 -0
- data/ext/bayeselo/CCDistributionCUI.h +38 -0
- data/ext/bayeselo/CCondensedResults.cpp +253 -0
- data/ext/bayeselo/CCondensedResults.h +64 -0
- data/ext/bayeselo/CDiscretization.h +40 -0
- data/ext/bayeselo/CDistribution.cpp +123 -0
- data/ext/bayeselo/CDistribution.h +58 -0
- data/ext/bayeselo/CDistributionCollection.cpp +34 -0
- data/ext/bayeselo/CDistributionCollection.h +29 -0
- data/ext/bayeselo/CEloRatingCUI.cpp +1046 -0
- data/ext/bayeselo/CEloRatingCUI.h +67 -0
- data/ext/bayeselo/CIndirectCompare.h +28 -0
- data/ext/bayeselo/CJointBayesian.cpp +111 -0
- data/ext/bayeselo/CJointBayesian.h +38 -0
- data/ext/bayeselo/CLUDecomposition.cpp +154 -0
- data/ext/bayeselo/CLUDecomposition.h +36 -0
- data/ext/bayeselo/CMatrix.cpp +48 -0
- data/ext/bayeselo/CMatrix.h +35 -0
- data/ext/bayeselo/CMatrixIO.cpp +27 -0
- data/ext/bayeselo/CMatrixIO.h +17 -0
- data/ext/bayeselo/CPredictionCUI.cpp +393 -0
- data/ext/bayeselo/CPredictionCUI.h +50 -0
- data/ext/bayeselo/CResultSet.cpp +245 -0
- data/ext/bayeselo/CResultSet.h +50 -0
- data/ext/bayeselo/CResultSetCUI.cpp +355 -0
- data/ext/bayeselo/CResultSetCUI.h +44 -0
- data/ext/bayeselo/CTimeIO.cpp +58 -0
- data/ext/bayeselo/CTimeIO.h +19 -0
- data/ext/bayeselo/CVector.cpp +110 -0
- data/ext/bayeselo/CVector.h +42 -0
- data/ext/bayeselo/EloDataFromFile.cpp +120 -0
- data/ext/bayeselo/EloDataFromFile.h +21 -0
- data/ext/bayeselo/README +8 -0
- data/ext/bayeselo/ReadLineToString.cpp +32 -0
- data/ext/bayeselo/ReadLineToString.h +18 -0
- data/ext/bayeselo/chtime.cpp +56 -0
- data/ext/bayeselo/chtime.h +61 -0
- data/ext/bayeselo/chtimer.h +27 -0
- data/ext/bayeselo/clktimer.cpp +178 -0
- data/ext/bayeselo/clktimer.h +29 -0
- data/ext/bayeselo/consolui.cpp +538 -0
- data/ext/bayeselo/consolui.h +154 -0
- data/ext/bayeselo/const.cpp +79 -0
- data/ext/bayeselo/const.h +42 -0
- data/ext/bayeselo/date.cpp +96 -0
- data/ext/bayeselo/date.h +44 -0
- data/ext/bayeselo/debug.h +58 -0
- data/ext/bayeselo/elomain.cpp +26 -0
- data/ext/bayeselo/extconf.rb +6 -0
- data/ext/bayeselo/list.h +367 -0
- data/ext/bayeselo/listi.h +125 -0
- data/ext/bayeselo/move.cpp +249 -0
- data/ext/bayeselo/move.h +139 -0
- data/ext/bayeselo/pgn.h +62 -0
- data/ext/bayeselo/pgnlex.cpp +432 -0
- data/ext/bayeselo/pgnlex.h +105 -0
- data/ext/bayeselo/pgnstr.cpp +126 -0
- data/ext/bayeselo/piece.h +44 -0
- data/ext/bayeselo/player.h +31 -0
- data/ext/bayeselo/position.h +89 -0
- data/ext/bayeselo/random.cpp +114 -0
- data/ext/bayeselo/random.h +63 -0
- data/ext/bayeselo/rb_bayeselo.cpp +76 -0
- data/ext/bayeselo/readstr.cpp +51 -0
- data/ext/bayeselo/readstr.h +19 -0
- data/ext/bayeselo/square.h +61 -0
- data/ext/bayeselo/str.cpp +81 -0
- data/ext/bayeselo/str.h +71 -0
- data/ext/bayeselo/version.cpp +30 -0
- data/ext/bayeselo/version.h +28 -0
- data/ext/bayeselo/version_number.h +1 -0
- data/lib/bayeselo.rb +6 -0
- data/lib/bayeselo/bayeselo.rb +11 -0
- data/lib/bayeselo/c_bayeselo.rb +7 -0
- data/test/bayeselo/test_bayeselo.rb +33 -0
- data/test/bayeselo/test_c_bayeselo.rb +84 -0
- data/test/helper.rb +18 -0
- metadata +226 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// pgnlex.cpp
|
|
4
|
+
//
|
|
5
|
+
// CPGNLex
|
|
6
|
+
//
|
|
7
|
+
// Remi Coulom
|
|
8
|
+
//
|
|
9
|
+
// August 1996
|
|
10
|
+
//
|
|
11
|
+
////////////////////////////////////////////////////////////////////////////
|
|
12
|
+
#include <iostream>
|
|
13
|
+
#include <cstdio>
|
|
14
|
+
|
|
15
|
+
#include "debug.h"
|
|
16
|
+
#include "pgnlex.h"
|
|
17
|
+
#include "str.h" // CSTR Class definition (for game terminations)
|
|
18
|
+
|
|
19
|
+
using namespace std;
|
|
20
|
+
|
|
21
|
+
//#define TRACETOKEN
|
|
22
|
+
#ifndef TRACETOKEN
|
|
23
|
+
#undef TRACE
|
|
24
|
+
#define TRACE(x)
|
|
25
|
+
#endif
|
|
26
|
+
static const char * const tszToken[] =
|
|
27
|
+
{
|
|
28
|
+
"TOK_BOF",
|
|
29
|
+
"TOK_EOF",
|
|
30
|
+
"TOK_String",
|
|
31
|
+
"TOK_Integer",
|
|
32
|
+
"TOK_Period",
|
|
33
|
+
"TOK_GameTermination",
|
|
34
|
+
"TOK_TagOpen",
|
|
35
|
+
"TOK_TagClose",
|
|
36
|
+
"TOK_RAVOpen",
|
|
37
|
+
"TOK_RAVClose",
|
|
38
|
+
"TOK_ReservedOpen",
|
|
39
|
+
"TOK_ReservedClose",
|
|
40
|
+
"TOK_NAG",
|
|
41
|
+
"TOK_Symbol",
|
|
42
|
+
"TOK_Comment",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
////////////////////////////////////////////////////////////////////////////
|
|
46
|
+
// Default constructor
|
|
47
|
+
////////////////////////////////////////////////////////////////////////////
|
|
48
|
+
CPGNLex::CPGNLex()
|
|
49
|
+
{
|
|
50
|
+
Reset();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
////////////////////////////////////////////////////////////////////////////
|
|
54
|
+
// Constructor
|
|
55
|
+
////////////////////////////////////////////////////////////////////////////
|
|
56
|
+
CPGNLex::CPGNLex(istream &istr)
|
|
57
|
+
{
|
|
58
|
+
Reset(istr);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
////////////////////////////////////////////////////////////////////////////
|
|
62
|
+
// Sets the current token to TOK_BOF
|
|
63
|
+
////////////////////////////////////////////////////////////////////////////
|
|
64
|
+
void CPGNLex::Reset()
|
|
65
|
+
{
|
|
66
|
+
Token = TOK_BOF;
|
|
67
|
+
Value = 0;
|
|
68
|
+
szString[0] = 0;
|
|
69
|
+
CurrChar = '\n';
|
|
70
|
+
ContextIndex = 0;
|
|
71
|
+
for (int i = ContextSize + 1; --i >= 0;)
|
|
72
|
+
szContextBuffer[i] = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
////////////////////////////////////////////////////////////////////////////
|
|
76
|
+
// Changes the input stream
|
|
77
|
+
////////////////////////////////////////////////////////////////////////////
|
|
78
|
+
void CPGNLex::Reset(istream &istr)
|
|
79
|
+
{
|
|
80
|
+
pistr = &istr;
|
|
81
|
+
|
|
82
|
+
Reset();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
////////////////////////////////////////////////////////////////////////////
|
|
86
|
+
// Reading of the next character from the input stream
|
|
87
|
+
////////////////////////////////////////////////////////////////////////////
|
|
88
|
+
int CPGNLex::NextChar()
|
|
89
|
+
{
|
|
90
|
+
if (pistr)
|
|
91
|
+
CurrChar = pistr->get();
|
|
92
|
+
else
|
|
93
|
+
CurrChar = EOF;
|
|
94
|
+
|
|
95
|
+
szContextBuffer[ContextIndex] = char(CurrChar);
|
|
96
|
+
ContextIndex = (ContextIndex + 1) % ContextSize;
|
|
97
|
+
szContextBuffer[ContextIndex] = 0;
|
|
98
|
+
|
|
99
|
+
return CurrChar;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
////////////////////////////////////////////////////////////////////////////
|
|
103
|
+
// Parse next token
|
|
104
|
+
////////////////////////////////////////////////////////////////////////////
|
|
105
|
+
int CPGNLex::ReadNextToken()
|
|
106
|
+
{
|
|
107
|
+
lblNextToken:
|
|
108
|
+
switch (CurrChar)
|
|
109
|
+
{
|
|
110
|
+
case EOF:
|
|
111
|
+
Token = TOK_EOF;
|
|
112
|
+
break;
|
|
113
|
+
|
|
114
|
+
//
|
|
115
|
+
// Separators and escape character
|
|
116
|
+
//
|
|
117
|
+
case '\n':
|
|
118
|
+
if (NextChar() == '%')
|
|
119
|
+
{
|
|
120
|
+
while (NextChar() != '\n')
|
|
121
|
+
if (CurrChar == EOF)
|
|
122
|
+
{
|
|
123
|
+
Token = TOK_EOF;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
NextChar();
|
|
127
|
+
}
|
|
128
|
+
goto lblNextToken;
|
|
129
|
+
|
|
130
|
+
case ' ': case '\t':
|
|
131
|
+
NextChar();
|
|
132
|
+
goto lblNextToken;
|
|
133
|
+
|
|
134
|
+
//
|
|
135
|
+
// Comments to EOL are parsed as separators
|
|
136
|
+
//
|
|
137
|
+
case ';':
|
|
138
|
+
while (NextChar() != '\n')
|
|
139
|
+
if (CurrChar == EOF)
|
|
140
|
+
{
|
|
141
|
+
Token = TOK_EOF;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
NextChar();
|
|
145
|
+
goto lblNextToken;
|
|
146
|
+
|
|
147
|
+
//
|
|
148
|
+
// Comments between braces are parsed as tokens
|
|
149
|
+
//
|
|
150
|
+
case '{':
|
|
151
|
+
{
|
|
152
|
+
int i = 0;
|
|
153
|
+
|
|
154
|
+
lblNextCharComment:
|
|
155
|
+
switch (NextChar())
|
|
156
|
+
{
|
|
157
|
+
case EOF:
|
|
158
|
+
{
|
|
159
|
+
Token = TOK_EOF;
|
|
160
|
+
goto lblReturn;
|
|
161
|
+
}
|
|
162
|
+
case '}':
|
|
163
|
+
szString[i] = 0;
|
|
164
|
+
NextChar();
|
|
165
|
+
break;
|
|
166
|
+
default:
|
|
167
|
+
if (CurrChar == '\n')
|
|
168
|
+
szString[i] = ' ';
|
|
169
|
+
else
|
|
170
|
+
szString[i] = CurrChar;
|
|
171
|
+
if (++i >= MaxPGNLine)
|
|
172
|
+
i = MaxPGNLine - 1;
|
|
173
|
+
goto lblNextCharComment;
|
|
174
|
+
}
|
|
175
|
+
Token = TOK_Comment;
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
|
|
179
|
+
//
|
|
180
|
+
// Strings
|
|
181
|
+
//
|
|
182
|
+
case '"':
|
|
183
|
+
{
|
|
184
|
+
int i = 0;
|
|
185
|
+
|
|
186
|
+
lblNextCharString:
|
|
187
|
+
switch (NextChar())
|
|
188
|
+
{
|
|
189
|
+
case EOF:
|
|
190
|
+
{
|
|
191
|
+
Token = TOK_EOF;
|
|
192
|
+
goto lblReturn;
|
|
193
|
+
}
|
|
194
|
+
case '"':
|
|
195
|
+
szString[i] = 0;
|
|
196
|
+
NextChar();
|
|
197
|
+
break;
|
|
198
|
+
case '\\':
|
|
199
|
+
NextChar();
|
|
200
|
+
default:
|
|
201
|
+
szString[i] = CurrChar;
|
|
202
|
+
if (++i >= MaxPGNLine)
|
|
203
|
+
i = MaxPGNLine - 1;
|
|
204
|
+
goto lblNextCharString;
|
|
205
|
+
}
|
|
206
|
+
Token = TOK_String;
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
|
|
210
|
+
//
|
|
211
|
+
// Tokens starting with digits
|
|
212
|
+
//
|
|
213
|
+
case '0': case '1': case '2': case '3': case '4':
|
|
214
|
+
case '5': case '6': case '7': case '8': case '9':
|
|
215
|
+
{
|
|
216
|
+
int PrevChar = CurrChar;
|
|
217
|
+
NextChar();
|
|
218
|
+
|
|
219
|
+
//
|
|
220
|
+
// Game termination
|
|
221
|
+
//
|
|
222
|
+
if (PrevChar == '1' && CurrChar == '-')
|
|
223
|
+
{
|
|
224
|
+
NextChar(); // 0
|
|
225
|
+
NextChar();
|
|
226
|
+
Value = CSTR::WhiteWins;
|
|
227
|
+
Token = TOK_GameTermination;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
if (PrevChar == '0' && CurrChar == '-')
|
|
231
|
+
{
|
|
232
|
+
NextChar(); // 1
|
|
233
|
+
NextChar();
|
|
234
|
+
Value = CSTR::BlackWins;
|
|
235
|
+
Token = TOK_GameTermination;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
if (PrevChar == '1' && CurrChar == '/')
|
|
239
|
+
{
|
|
240
|
+
NextChar(); // 2
|
|
241
|
+
NextChar(); // -
|
|
242
|
+
NextChar(); // 1
|
|
243
|
+
NextChar(); // /
|
|
244
|
+
NextChar(); // 2
|
|
245
|
+
NextChar();
|
|
246
|
+
Value = CSTR::Draw;
|
|
247
|
+
Token = TOK_GameTermination;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
//
|
|
252
|
+
// Integers
|
|
253
|
+
//
|
|
254
|
+
Value = PrevChar - '0';
|
|
255
|
+
while ('0' <= CurrChar && CurrChar <= '9')
|
|
256
|
+
{
|
|
257
|
+
Value = Value * 10 + CurrChar - '0';
|
|
258
|
+
NextChar();
|
|
259
|
+
}
|
|
260
|
+
Token = TOK_Integer;
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
|
|
264
|
+
//
|
|
265
|
+
// NAG
|
|
266
|
+
//
|
|
267
|
+
case '$':
|
|
268
|
+
Value = -1;
|
|
269
|
+
NextChar();
|
|
270
|
+
Token = TOK_NAG;
|
|
271
|
+
break;
|
|
272
|
+
|
|
273
|
+
case '?':
|
|
274
|
+
Value = 2;
|
|
275
|
+
switch(NextChar())
|
|
276
|
+
{
|
|
277
|
+
case '?':
|
|
278
|
+
Value = 4;
|
|
279
|
+
NextChar();
|
|
280
|
+
break;
|
|
281
|
+
|
|
282
|
+
case '!':
|
|
283
|
+
Value = 6;
|
|
284
|
+
NextChar();
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
Token = TOK_NAG;
|
|
288
|
+
break;
|
|
289
|
+
|
|
290
|
+
case '!':
|
|
291
|
+
Value = 1;
|
|
292
|
+
switch(NextChar())
|
|
293
|
+
{
|
|
294
|
+
case '?':
|
|
295
|
+
Value = 5;
|
|
296
|
+
NextChar();
|
|
297
|
+
break;
|
|
298
|
+
|
|
299
|
+
case '!':
|
|
300
|
+
Value = 3;
|
|
301
|
+
NextChar();
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
Token = TOK_NAG;
|
|
305
|
+
break;
|
|
306
|
+
|
|
307
|
+
//
|
|
308
|
+
// Single-character tokens
|
|
309
|
+
//
|
|
310
|
+
case '*':
|
|
311
|
+
NextChar();
|
|
312
|
+
Value = CSTR::Unknown;
|
|
313
|
+
Token = TOK_GameTermination;
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
case '.':
|
|
317
|
+
NextChar();
|
|
318
|
+
Token = TOK_Period;
|
|
319
|
+
break;
|
|
320
|
+
|
|
321
|
+
case '[':
|
|
322
|
+
NextChar();
|
|
323
|
+
Token = TOK_TagOpen;
|
|
324
|
+
break;
|
|
325
|
+
|
|
326
|
+
case ']':
|
|
327
|
+
NextChar();
|
|
328
|
+
Token = TOK_TagClose;
|
|
329
|
+
break;
|
|
330
|
+
|
|
331
|
+
case '(':
|
|
332
|
+
NextChar();
|
|
333
|
+
Token = TOK_RAVOpen;
|
|
334
|
+
break;
|
|
335
|
+
|
|
336
|
+
case ')':
|
|
337
|
+
NextChar();
|
|
338
|
+
Token = TOK_RAVClose;
|
|
339
|
+
break;
|
|
340
|
+
|
|
341
|
+
case '<':
|
|
342
|
+
NextChar();
|
|
343
|
+
Token = TOK_ReservedOpen;
|
|
344
|
+
break;
|
|
345
|
+
|
|
346
|
+
case '>':
|
|
347
|
+
NextChar();
|
|
348
|
+
Token = TOK_ReservedClose;
|
|
349
|
+
break;
|
|
350
|
+
|
|
351
|
+
//
|
|
352
|
+
// Symbols
|
|
353
|
+
//
|
|
354
|
+
default:
|
|
355
|
+
if (('a' <= CurrChar && CurrChar <= 'z') ||
|
|
356
|
+
('A' <= CurrChar && CurrChar <= 'Z'))
|
|
357
|
+
{
|
|
358
|
+
int i = 0;
|
|
359
|
+
|
|
360
|
+
do
|
|
361
|
+
{
|
|
362
|
+
szString[i] = CurrChar;
|
|
363
|
+
if (++i >= MaxPGNLine)
|
|
364
|
+
i = MaxPGNLine - 1;
|
|
365
|
+
NextChar();
|
|
366
|
+
}
|
|
367
|
+
while (('a' <= CurrChar && CurrChar <= 'z') ||
|
|
368
|
+
('A' <= CurrChar && CurrChar <= 'Z') ||
|
|
369
|
+
('0' <= CurrChar && CurrChar <= '9') ||
|
|
370
|
+
CurrChar == '_' ||
|
|
371
|
+
CurrChar == '=' ||
|
|
372
|
+
CurrChar == ':' ||
|
|
373
|
+
CurrChar == '-' ||
|
|
374
|
+
CurrChar == '+' ||
|
|
375
|
+
CurrChar == '#');
|
|
376
|
+
|
|
377
|
+
szString[i] = 0;
|
|
378
|
+
Token = TOK_Symbol;
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
else // Unexpected characters are skipped
|
|
382
|
+
{
|
|
383
|
+
NextChar();
|
|
384
|
+
goto lblNextToken;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
lblReturn:
|
|
389
|
+
|
|
390
|
+
TRACE(tszToken[Token] << ' ' << Value << ' ' << szString);
|
|
391
|
+
|
|
392
|
+
return Token;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
////////////////////////////////////////////////////////////////////////////
|
|
396
|
+
// Access to the previously read token value
|
|
397
|
+
////////////////////////////////////////////////////////////////////////////
|
|
398
|
+
int CPGNLex::TokenValue() const
|
|
399
|
+
{
|
|
400
|
+
return Value;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
////////////////////////////////////////////////////////////////////////////
|
|
404
|
+
// Access to the previously read token string
|
|
405
|
+
////////////////////////////////////////////////////////////////////////////
|
|
406
|
+
const char *CPGNLex::TokenString() const
|
|
407
|
+
{
|
|
408
|
+
return szString;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
////////////////////////////////////////////////////////////////////////////
|
|
412
|
+
// WaitForToken
|
|
413
|
+
////////////////////////////////////////////////////////////////////////////
|
|
414
|
+
int CPGNLex::WaitForToken(int Searched)
|
|
415
|
+
{
|
|
416
|
+
while (Token != Searched)
|
|
417
|
+
if (ReadNextToken() == TOK_EOF)
|
|
418
|
+
return 1;
|
|
419
|
+
|
|
420
|
+
return 0;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
////////////////////////////////////////////////////////////////////////////
|
|
424
|
+
// PrintContext
|
|
425
|
+
////////////////////////////////////////////////////////////////////////////
|
|
426
|
+
void CPGNLex::PrintContext(std::ostream &out) const
|
|
427
|
+
{
|
|
428
|
+
out << szContextBuffer + ContextIndex + 1 << szContextBuffer << '\n';
|
|
429
|
+
out << "Token : " << tszToken[Token] << '\n';
|
|
430
|
+
out << "Value : " << Value << '\n';
|
|
431
|
+
out << "String : " << szString << '\n';
|
|
432
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// pgnlex.h
|
|
4
|
+
//
|
|
5
|
+
// CPGNLex class definition
|
|
6
|
+
// (PGN lexical analysis)
|
|
7
|
+
//
|
|
8
|
+
// Remi Coulom
|
|
9
|
+
//
|
|
10
|
+
// August 1996
|
|
11
|
+
//
|
|
12
|
+
////////////////////////////////////////////////////////////////////////////
|
|
13
|
+
#ifndef PGNLEX_H
|
|
14
|
+
#define PGNLEX_H
|
|
15
|
+
|
|
16
|
+
#include <iostream>
|
|
17
|
+
|
|
18
|
+
class CPGNLex //pgnlex
|
|
19
|
+
{
|
|
20
|
+
private: //////////////////////////////////////////////////////////////////
|
|
21
|
+
std::istream *pistr;
|
|
22
|
+
|
|
23
|
+
int CurrChar;
|
|
24
|
+
int Token;
|
|
25
|
+
int Value;
|
|
26
|
+
enum {MaxPGNLine = 1024};
|
|
27
|
+
char szString[MaxPGNLine];
|
|
28
|
+
enum {ContextSize = 40};
|
|
29
|
+
char szContextBuffer[ContextSize + 1];
|
|
30
|
+
int ContextIndex;
|
|
31
|
+
|
|
32
|
+
int NextChar();
|
|
33
|
+
|
|
34
|
+
public: ///////////////////////////////////////////////////////////////////
|
|
35
|
+
//
|
|
36
|
+
// Tokens
|
|
37
|
+
// Note : these tokens do not conform strictly with standard.txt
|
|
38
|
+
//
|
|
39
|
+
// DIGIT = [0-9]
|
|
40
|
+
// LETTER = [a-zA-Z]
|
|
41
|
+
// SYMBOLCONTINUATION = LETTER U DIGIT U {_} U {+} U {#} U {=} U {:} U {-}
|
|
42
|
+
//
|
|
43
|
+
enum
|
|
44
|
+
{
|
|
45
|
+
TOK_BOF, // Beginning Of File
|
|
46
|
+
TOK_EOF, // End Of File
|
|
47
|
+
TOK_String, //s '"'(ALLCHARACTERS U '\\' U '\"' - '\' - '"')*'"'
|
|
48
|
+
TOK_Integer, //n DIGIT+
|
|
49
|
+
TOK_Period, // '.'
|
|
50
|
+
TOK_GameTermination, //n '0-1' U '1/2-1/2' U '1-0' U '*'
|
|
51
|
+
TOK_TagOpen, // '['
|
|
52
|
+
TOK_TagClose, // ']'
|
|
53
|
+
TOK_RAVOpen, // '('
|
|
54
|
+
TOK_RAVClose, // ')'
|
|
55
|
+
TOK_ReservedOpen, // '<'
|
|
56
|
+
TOK_ReservedClose, // '>'
|
|
57
|
+
TOK_NAG, //n '$'DIGIT* U '?' U '!' U '!?' U '?!' U '!!' U '??'
|
|
58
|
+
TOK_Symbol, //s LETTER(SYMBOLCONTINUATION)*
|
|
59
|
+
TOK_Comment, //s '{'(ALLCHARACTERS - '}')*'}'
|
|
60
|
+
TOKs
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//
|
|
64
|
+
// Constructor
|
|
65
|
+
//
|
|
66
|
+
CPGNLex();
|
|
67
|
+
CPGNLex(std::istream &istr);
|
|
68
|
+
|
|
69
|
+
//
|
|
70
|
+
// Function to reset the input stream
|
|
71
|
+
//
|
|
72
|
+
|
|
73
|
+
// Sets the current token to TOK_BOF
|
|
74
|
+
void Reset();
|
|
75
|
+
// Changes the input stream
|
|
76
|
+
void Reset(std::istream &istr);
|
|
77
|
+
|
|
78
|
+
//
|
|
79
|
+
// Functions to read tokens
|
|
80
|
+
//
|
|
81
|
+
|
|
82
|
+
// Function to move to the next token
|
|
83
|
+
int ReadNextToken();
|
|
84
|
+
|
|
85
|
+
// Access to the current token type
|
|
86
|
+
int GetToken() const {return Token;}
|
|
87
|
+
|
|
88
|
+
// Access to the current token value
|
|
89
|
+
// This value is set when TOK_Integer, TOK_GameTermination, TOK_NAG read
|
|
90
|
+
int TokenValue() const;
|
|
91
|
+
|
|
92
|
+
// Access to the current token string
|
|
93
|
+
// This value is set when a TOK_String or TOK_Symbol is read
|
|
94
|
+
const char *TokenString() const;
|
|
95
|
+
|
|
96
|
+
// returns 0 if the searched token is found, 1 if eof is reached
|
|
97
|
+
int WaitForToken(int Searched);
|
|
98
|
+
|
|
99
|
+
//
|
|
100
|
+
// Get context (useful to display error messages)
|
|
101
|
+
//
|
|
102
|
+
void PrintContext(std::ostream &out) const;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
#endif
|