4 * Copyright 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
5 * ------------------------------------------------------------------------
7 * GNU XBoard is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at
10 * your option) any later version.
12 * GNU XBoard is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see http://www.gnu.org/licenses/. *
20 *------------------------------------------------------------------------
21 ** See the file ChangeLog for a revision history. */
34 extern Board boards[MAX_MOVES];
35 extern int PosFlags(int nr);
37 int yyskipmoves = FALSE;
38 char currentMoveString[4096]; // a bit ridiculous size?
41 #define PARSEBUFSIZE 10000
43 static FILE *inputFile;
44 static char *inPtr, *parsePtr, *parseStart;
45 static char inputBuf[PARSEBUFSIZE];
46 static char yytext[PARSEBUFSIZE];
47 static char fromString = 0, lastChar = '\n';
52 #define BADNUMBER (-2000000000)
56 { // Read one line from the input file, and append to the buffer
57 char c, *start = inPtr;
58 if(fromString) return 0; // parsing string, so the end is a hard end
59 if(!inputFile) return 0;
60 while((c = fgetc(inputFile)) != EOF) {
62 if(c == '\n') { *inPtr = NULLCHAR; return 1; }
63 if(inPtr - inputBuf > PARSEBUFSIZE-2) inPtr--; //prevent crash on overflow
65 if(inPtr == start) return 0;
66 *inPtr++ = '\n', *inPtr = NULLCHAR; // repair missing linefeed at EOF
71 Scan (char c, char **p)
72 { // line-spanning skip to mentioned character or EOF
74 while(**p) if(*(*p)++ == c) return 0;
76 // no closing bracket; force match for entire rest of file.
82 { // skip spaces tabs and newlines; return 1 if anything was skipped
85 while(**p == ' ' || **p == '\t' || **p == '\n' || **p == '\r') (*p)++;
86 } while(**p == NULLCHAR && ReadLine()); // continue as long as ReadLine reads something
91 Match (char *pattern, char **ptr)
93 char *p = pattern, *s = *ptr;
94 while(*p && (*p == *s++ || s[-1] == '\r' && *p--)) p++;
99 return 0; // no match, no ptr update
103 Word (char *pattern, char **p)
105 if(Match(pattern, p)) return 1;
106 if(*pattern >= 'a' && *pattern <= 'z' && *pattern - **p == 'a' - 'A') { // capitalized
108 if(Match(pattern + 1, p)) return 1;
115 Verb (char *pattern, char **p)
117 int res = Word(pattern, p);
118 if(res && !Match("s", p)) Match("ed", p); // eat conjugation suffix, if any
126 if(**p < '0' || **p > '9') return BADNUMBER;
127 while(**p >= '0' && **p <= '9') {
128 val = 10*val + *(*p)++ - '0';
134 RdTime (char c, char **p)
136 char *start = ++(*p), *sec; // increment *p, as it was pointing to the opening ( or {
137 if(Number(p) == BADNUMBER) return 0;
139 if(Match(":", p) && Number(p) != BADNUMBER && *p - sec == 3) { // well formed
141 if(Match(".", p) && Number(p) != BADNUMBER && *(*p)++ == c) return 1; // well-formed fraction
143 if(*(*p)++ == c) return 1; // matching bracket without fraction
145 *p = start; // failure
150 PromoSuffix (char **p)
153 if(**p == 'e' && (Match("ep", p) || Match("e.p.", p))) { *p = start; return NULLCHAR; } // non-compliant e.p. suffix is no promoChar!
154 if(**p == '+' && IS_SHOGI(gameInfo.variant)) { (*p)++; return '+'; }
155 if(**p == '=' || (gameInfo.variant == VariantSChess) && **p == '/') (*p)++; // optional = (or / for Seirawan gating)
156 if(**p == '(' && (*p)[2] == ')' && isalpha( (*p)[1] )) { (*p) += 3; return ToLower((*p)[-2]); }
157 if(isalpha(**p) && **p != 'x') return ToLower(*(*p)++); // reserve 'x' for multi-leg captures?
158 if(*p != start) return **p == '+' ? *(*p)++ : '='; // must be the optional = (or =+)
159 return NULLCHAR; // no suffix detected
164 { // Main parser routine
165 int coord[4], n, result, piece, i;
166 char type[4], promoted, separator, slash, *oldp, *commentEnd, c;
167 int wom = quickFlag ? quickFlag&1 : WhiteOnMove(yyboardindex);
169 // ********* try white first, because it is so common **************************
170 if(**p == ' ' || **p == '\n' || **p == '\t') { parseStart = (*p)++; return Nothing; }
173 if(**p == NULLCHAR) { // make sure there is something to parse
174 if(fromString) return 0; // we are parsing string, so the end is really the end
175 *p = inPtr = inputBuf;
176 if(!ReadLine()) return 0; // EOF
177 } else if(inPtr > inputBuf + PARSEBUFSIZE/2) { // buffer fills up with already parsed stuff
178 char *q = *p, *r = inputBuf;
180 *p = inputBuf; inPtr = r - 1;
182 parseStart = oldp = *p; // remember where we begin
185 // ********* attempt to recognize a SAN move in the leading non-blank text *****
186 piece = separator = promoted = slash = n = 0;
187 for(i=0; i<4; i++) coord[i] = -1, type[i] = NOTHING;
188 if(**p == '+') (*p)++, promoted++;
189 if(**p >= 'a' && **p <= 'z' && (*p)[1]== '@') piece =*(*p)++ + 'A' - 'a'; else
190 if(**p >= 'A' && **p <= 'Z') {
191 piece = *(*p)++; // Note we could test for 2-byte non-ascii names here
192 if(**p == '/') slash = *(*p)++;
195 if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC;
196 else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;
198 if(n == 2 && type[0] == type[1]) { // if two identical types, the opposite type in between must have been missing
199 type[2] = type[1]; coord[2] = coord[1];
200 type[1] = NOTHING; coord[1] = -1; n++;
203 // we always get here, and might have read a +, a piece, and upto 4 potential coordinates
204 if(n <= 2) { // could be from-square or disambiguator, when -:xX follow, or drop with @ directly after piece, but also to-square
205 if(**p == '-' || **p == ':' || **p == 'x' || **p == 'X' || // these cannot be move suffix, so to-square must follow
206 (**p == '@' || **p == '*') && n == 0 && !promoted && piece) { // P@ must also be followed by to-square
208 if(n == 1) coord[1] = coord[0]; // must be disambiguator, but we do not know which yet
210 while(n < 4) { // attempt to read to-square
211 if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC;
212 else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;
215 } else if((**p == '+' || **p == '=') && n == 1 && piece && type[0] == NUMERIC) { // can be traditional Xiangqi notation
218 if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;
219 } else if(n == 2) { // only one square mentioned, must be to-square
220 while(n < 4) { coord[n] = coord[n-2], type[n] = type[n-2], coord[n-2] = -1, type[n-2] = NOTHING; n++; }
222 } else if(n == 3 && type[1] != NOTHING) { // must be hyphenless disambiguator + to-square
223 for(i=3; i>0; i--) coord[i] = coord[i-1], type[i] = type[i-1]; // move to-square to where it belongs
224 type[1] = NOTHING; // disambiguator goes in first two positions
227 // we always get here; move must be completely read now, with to-square coord(s) at end
228 if(n == 3) { // incomplete to-square. Could be Xiangqi traditional, or stuff like fxg
229 if(piece && type[1] == NOTHING && type[0] == NUMERIC && type[2] == NUMERIC &&
230 (separator == '+' || separator == '=' || separator == '-')) {
231 // Xiangqi traditional
233 return ImpossibleMove; // for now treat as invalid
235 // fxg stuff, but also things like 0-0, 0-1 and 1-0
236 if(!piece && type[1] == NOTHING && type[0] == ALPHABETIC && type[2] == ALPHABETIC
237 && (coord[0] != 14 || coord[2] != 14) /* reserve oo for castling! */ ) {
238 piece = 'P'; n = 4; // kludge alert: fake full to-square
240 } else if(n == 1 && type[0] == NUMERIC && coord[0] > 1) { while(**p == '.') (*p)++; return Nothing; } // fast exit for move numbers
241 if(n == 4 && type[2] != type[3] && // we have a valid to-square (kludge: type[3] can be NOTHING on fxg type move)
242 (piece || !promoted) && // promoted indicator only valid on named piece type
243 (type[2] == ALPHABETIC || IS_SHOGI(gameInfo.variant))) { // in Shogi also allow alphabetic rank
244 DisambiguateClosure cl;
245 int fromX, fromY, toX, toY;
247 if(slash && (!piece || type[1] == NOTHING)) goto badMove; // slash after piece only in ICS long format
248 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
250 if(type[2] == NUMERIC) { // alpha-rank
251 coord[2] = BOARD_RGHT - BOARD_LEFT - coord[2];
252 coord[3] = BOARD_HEIGHT - coord[3];
253 if(coord[0] >= 0) coord[0] = BOARD_RGHT - BOARD_LEFT - coord[0];
254 if(coord[1] >= 0) coord[1] = BOARD_HEIGHT - coord[1];
256 toX = cl.ftIn = (currentMoveString[2] = coord[2] + 'a') - AAA;
257 toY = cl.rtIn = (currentMoveString[3] = coord[3] + '0') - ONE;
258 if(type[3] == NOTHING) cl.rtIn = -1; // for fxg type moves ask for toY disambiguation
259 else if(toY >= BOARD_HEIGHT || toY < 0) return ImpossibleMove; // vert off-board to-square
260 if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return ImpossibleMove;
262 cl.pieceIn = CharToPiece(wom ? piece : ToLower(piece));
263 if(cl.pieceIn == EmptySquare) return ImpossibleMove; // non-existent piece
264 if(promoted) cl.pieceIn = (ChessSquare) (CHUPROMOTED cl.pieceIn);
265 } else cl.pieceIn = EmptySquare;
266 if(separator == '@' || separator == '*') { // drop move. We only get here without from-square or promoted piece
267 fromY = DROP_RANK; fromX = cl.pieceIn;
268 currentMoveString[0] = piece;
269 currentMoveString[1] = '@';
270 currentMoveString[4] = NULLCHAR;
271 return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, fromY, fromX, toY, toX, NULLCHAR);
273 if(type[1] == NOTHING && type[0] != NOTHING) { // there is a disambiguator
274 if(type[0] != type[2]) coord[0] = -1, type[1] = type[0], type[0] = NOTHING; // it was a rank-disambiguator
276 if( type[1] != type[2] && // means fromY is of opposite type as ToX, or NOTHING
277 (type[0] == NOTHING || type[0] == type[2]) ) { // well formed
279 fromX = (currentMoveString[0] = coord[0] + 'a') - AAA;
280 fromY = (currentMoveString[1] = coord[1] + '0') - ONE;
281 currentMoveString[4] = cl.promoCharIn = PromoSuffix(p);
282 currentMoveString[5] = NULLCHAR;
283 if(!cl.promoCharIn && (**p == '-' || **p == 'x')) { // Lion-type multi-leg move
284 currentMoveString[5] = (killX = toX) + AAA; // what we thought was to-square is in fact kill-square
285 currentMoveString[6] = (killY = toY) + ONE; // append it as suffix behind long algebraic move
286 currentMoveString[4] = ';';
287 currentMoveString[7] = NULLCHAR;
288 // read new to-square (VERY non-robust! Assumes correct (non-alpha-rank) syntax, and messes up on errors)
289 toX = cl.ftIn = (currentMoveString[2] = *++*p) - AAA; ++*p;
290 toY = cl.rtIn = (currentMoveString[3] = Number(p) + '0') - ONE;
292 if(type[0] != NOTHING && type[1] != NOTHING && type[3] != NOTHING) { // fully specified.
293 ChessSquare realPiece = boards[yyboardindex][fromY][fromX];
294 // Note that Disambiguate does not work for illegal moves, but flags them as impossible
295 if(piece) { // check if correct piece indicated
296 if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED realPiece);
297 if(!(appData.icsActive && PieceToChar(realPiece) == '+') && // trust ICS if it moves promoted pieces
298 piece && realPiece != cl.pieceIn) return ImpossibleMove;
299 } else if(!separator && **p == '+') { // could be a protocol move, where bare '+' suffix means shogi-style promotion
300 if(realPiece < (wom ? WhiteCannon : BlackCannon) && PieceToChar(PROMOTED realPiece) == '+') // seems to be that
301 currentMoveString[4] = cl.promoCharIn = *(*p)++; // append promochar after all
303 result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), fromY, fromX, toY, toX, cl.promoCharIn);
304 if (currentMoveString[4] == NULLCHAR) { // suppy missing mandatory promotion character
305 if(result == WhitePromotion || result == BlackPromotion) {
306 switch(gameInfo.variant) {
308 case VariantShatranj: currentMoveString[4] = PieceToChar(BlackFerz); break;
309 case VariantGreat: currentMoveString[4] = PieceToChar(BlackMan); break;
310 case VariantShogi: currentMoveString[4] = '+'; break;
311 default: currentMoveString[4] = PieceToChar(BlackQueen);
313 } else if(result == WhiteNonPromotion || result == BlackNonPromotion) {
314 currentMoveString[4] = '=';
316 } else if(appData.testLegality && gameInfo.variant != VariantSChess && // strip off unnecessary and false promo characters
317 !(result == WhitePromotion || result == BlackPromotion ||
318 result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
320 } else if(cl.pieceIn == EmptySquare) cl.pieceIn = wom ? WhitePawn : BlackPawn;
321 cl.ffIn = type[0] == NOTHING ? -1 : coord[0] + 'a' - AAA;
322 cl.rfIn = type[1] == NOTHING ? -1 : coord[1] + '0' - ONE;
324 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
326 if(cl.kind == ImpossibleMove && !piece && type[1] == NOTHING // fxg5 type
327 && toY == (wom ? 4 : 3)) { // could be improperly written e.p.
328 cl.rtIn += wom ? 1 : -1; // shift target square to e.p. square
329 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
330 if((cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant))
331 return ImpossibleMove; // nice try, but no cigar
334 currentMoveString[0] = cl.ff + AAA;
335 currentMoveString[1] = cl.rf + ONE;
336 currentMoveString[3] = cl.rt + ONE;
337 if(killX < 0) // [HGM] lion: do not overwrite kill-square suffix
338 currentMoveString[4] = cl.promoChar;
340 if((cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) && (Match("ep", p) || Match("e.p.", p)));
342 return (int) cl.kind;
345 badMove:// we failed to find algebraic move
349 // Next we do some common symbols where the first character commits us to things that cannot possibly be a move
351 // ********* PGN tags ******************************************
354 if(Match("--", p)) { // "[--" could be start of position diagram
355 if(!Scan(']', p) && (*p)[-3] == '-' && (*p)[-2] == '-') return PositionDiagram;
359 if(isdigit(**p) || isalpha(**p)) {
360 do (*p)++; while(isdigit(**p) || isalpha(**p) || **p == '+' ||
361 **p == '-' || **p == '=' || **p == '_' || **p == '#');
365 while(**p != '\n' && (*(*p)++ != '"'|| (*p)[-2] == '\\')); // look for unescaped quote
366 if((*p)[-1] !='"') { *p = oldp; Scan(']', p); return Comment; } // string closing delimiter missing
367 SkipWhite(p); if(*(*p)++ == ']') return PGNTag;
370 Scan(']', p); return Comment;
373 // ********* SAN Castings *************************************
374 if(**p == 'O' || **p == 'o' || **p == '0' && !Match("00:", p)) { // exclude 00 in time stamps
375 int castlingType = 0;
376 if(Match("O-O-O", p) || Match("o-o-o", p) || Match("0-0-0", p) ||
377 Match("OOO", p) || Match("ooo", p) || Match("000", p)) castlingType = 2;
378 else if(Match("O-O", p) || Match("o-o", p) || Match("0-0", p) ||
379 Match("OO", p) || Match("oo", p) || Match("00", p)) castlingType = 1;
380 if(castlingType) { //code from old parser, collapsed for both castling types, and streamlined a bit
381 int rf, ff, rt, ft; ChessSquare king;
384 if(gameInfo.variant == VariantSChess) promo = PromoSuffix(p);
386 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
397 ff = (BOARD_WIDTH-1)>>1; // this would be d-file
398 if (boards[yyboardindex][rf][ff] == king) {
399 /* ICS wild castling */
400 ft = castlingType == 1 ? BOARD_LEFT+1 : (gameInfo.variant == VariantJanus ? BOARD_RGHT-2 : BOARD_RGHT-3);
402 ff = BOARD_WIDTH>>1; // e-file
403 ft = castlingType == 1 ? BOARD_RGHT-2 : BOARD_LEFT+2;
405 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
407 ff = initialRights[2];
408 ft = initialRights[castlingType-1];
410 ff = initialRights[5];
411 ft = initialRights[castlingType+2];
413 if (appData.debugMode) fprintf(debugFP, "Parser FRC (type=%d) %d %d\n", castlingType, ff, ft);
414 if(ff == NoRights || ft == NoRights) return ImpossibleMove;
416 sprintf(currentMoveString, "%c%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE,promo);
417 if (appData.debugMode) fprintf(debugFP, "(%d-type) castling %d %d\n", castlingType, ff, ft);
419 return (int) LegalityTest(boards[yyboardindex],
420 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
421 rf, ff, rt, ft, promo);
426 // ********* variations (nesting) ******************************
428 if(RdTime(')', p)) return ElapsedTime;
431 if(**p ==')') { (*p)++; return Close; }
432 if(**p == ';') { while(**p != '\n') (*p)++; return Comment; }
435 // ********* Comments and result messages **********************
436 *p = oldp; commentEnd = NULL; result = 0;
438 if(RdTime('}', p)) return ElapsedTime;
439 if(lastChar == '\n' && Match("--------------\n", p)) {
441 i = Scan ('}', p); q = *p - 16;
442 if(Match("\n--------------}\n", &q)) return PositionDiagram;
443 } else i = Scan('}', p);
444 commentEnd = *p; if(i) return Comment; // return comment that runs to EOF immediately
446 if(commentEnd) SkipWhite(p);
447 if(Match("*", p)) result = GameUnfinished;
448 else if(**p == '0') {
449 if( Match("0-1", p) || Match("0/1", p) || Match("0:1", p) ||
450 Match("0 - 1", p) || Match("0 / 1", p) || Match("0 : 1", p)) result = BlackWins;
451 } else if(**p == '1') {
452 if( Match("1-0", p) || Match("1/0", p) || Match("1:0", p) ||
453 Match("1 - 0", p) || Match("1 / 0", p) || Match("1 : 0", p)) result = WhiteWins;
454 else if(Match("1/2 - 1/2", p) || Match("1/2:1/2", p) || Match("1/2 : 1/2", p) || Match("1 / 2 - 1 / 2", p) ||
455 Match("1 / 2 : 1 / 2", p) || Match("1/2", p) || Match("1 / 2", p)) result = GameIsDrawn;
458 if(Match(" (", p) && !Scan(')', p) || Match(" {", p) && !Scan('}', p)) { // there is a comment after the PGN result!
459 if(commentEnd) { *p = commentEnd; return Comment; } // so comment before it is normal comment; return that first
461 return result; // this returns a possible preceeding comment as result details
463 if(commentEnd) { *p = commentEnd; return Comment; } // there was no PGN result following, so return as normal comment
466 // ********* Move numbers (after castlings or PGN results!) ***********
467 if((i = Number(p)) != BADNUMBER) { // a single number was read as part of our attempt to read a move
469 if(**p == '.') (*p)++; SkipWhite(p);
470 if(**p == '+' || isalpha(**p) || gameInfo.variant == VariantShogi && *p != numEnd && isdigit(**p)) {
472 return i == 1 ? MoveNumberOne : Nothing;
474 *p = numEnd; return Nothing;
478 // ********* non-compliant game-result indicators *********************
479 if(Match("+-+", p) || Word("stalemate", p)) return GameIsDrawn;
480 if(Match("++", p) || Verb("resign", p) || (Word("check", p) || 1) && Word("mate", p) )
481 return (wom ? BlackWins : WhiteWins);
483 if(Word("w", p) && (Match("hite", p) || 1) || Word("b", p) && (Match("lack", p) || 1) ) {
484 if(**p != ' ') return Nothing;
486 if(Verb("disconnect", p)) return GameUnfinished;
487 if(Verb("resign", p) || Verb("forfeit", p) || Word("mated", p) || Word("lost", p) || Word("loses", p))
488 return (c == 'W' ? BlackWins : WhiteWins);
489 if(Word("mates", p) || Word("wins", p) || Word("won", p))
490 return (c != 'W' ? BlackWins : WhiteWins);
493 if(Word("draw", p)) {
494 if(**p == 'n') (*p)++;
495 if(**p != ' ') return GameIsDrawn;
497 if(Word("agreed", p)) return GameIsDrawn;
498 if(Match("by ", p) && (Word("repetition", p) || Word("agreement", p)) ) return GameIsDrawn;
501 while(**p != '\n') if(*(*p)++ == ')') break;
502 if((*p)[-1] == ')') return GameIsDrawn;
504 *p = oldp - 1; return GameIsDrawn;
508 // ********* Numeric annotation glyph **********************************
509 if(**p == '$') { (*p)++; if(Number(p) != BADNUMBER) return NAG; return Nothing; }
512 // ********** by now we are getting down to the silly stuff ************
513 if(Word("gnu", p) || Match("GNU", p)) {
514 if(**p == ' ') (*p)++;
515 if(Word("chess", p) || Match("CHESS", p)) {
517 if((q = strstr(*p, "game")) || (q = strstr(*p, "GAME")) || (q = strstr(*p, "Game"))) {
518 (*p) = q + 4; return GNUChessGame;
523 if(lastChar == '\n' && (Match("# ", p) || Match("; ", p) || Match("% ", p))) {
524 while(**p != '\n' && **p != ' ') (*p)++;
525 if(**p == ' ' && (Match(" game file", p) || Match(" position file", p))) {
526 while(**p != '\n') (*p)++; // skip to EOLN
529 *p = oldp; // we might need to re-match the skipped stuff
532 if(Match("@@@@", p) || Match("--", p) || Match("Z0", p) || Match("pass", p) || Match("null", p)) {
533 strncpy(currentMoveString, "@@@@", 5);
534 return yyboardindex & F_WHITE_ON_MOVE ? WhiteDrop : BlackDrop;
537 // ********* Efficient skipping of (mostly) alphabetic chatter **********
538 while(isdigit(**p) || isalpha(**p) || **p == '-') (*p)++;
541 while(isdigit(**p) || isalpha(**p) || **p == '-' || **p == '\'') (*p)++;
542 return Nothing; // random word
544 if(lastChar == '\n' && Match(": ", p)) { // mail header, skip indented lines
546 while(**p != '\n') (*p)++;
547 if(!ReadLine()) return Nothing; // append next line if not EOF
548 } while(Match("\n ", p) || Match("\n\t", p));
553 // ********* Prevent 00 in unprotected time stamps to be mistaken for castling *******
554 if(Match(":00", p)) return Nothing;
556 // ********* Could not match to anything. Return offending character ****
562 Return offset of next pattern in the current file.
567 return ftell(inputFile) - (inPtr - parsePtr); // subtract what is read but not yet parsed
572 { // prepare parse buffer for reading file
574 inPtr = parsePtr = inputBuf;
577 *inPtr = NULLCHAR; // make sure we will start by reading a line
581 yynewstr P((char *s))
590 { // this replaces the flex-generated parser
591 int result = NextUnit(&parsePtr);
592 char *p = parseStart, *q = yytext;
593 while(p < parsePtr) *q++ = *p++; // copy the matched text to yytext[]
601 { // [HGM] wrapper for yylex, which treats nesting of parentheses
602 int symbol, nestingLevel = 0, i=0;
604 static char buf[256*MSG_SIZ];
606 do { // eat away anything not at level 0
608 if(symbol == Open) nestingLevel++;
609 if(nestingLevel) { // save all parsed text between (and including) the ()
610 for(p=yytext; *p && i<256*MSG_SIZ-2;) buf[i++] = *p++;
613 if(symbol == 0) break; // ran into EOF
614 if(symbol == Close) symbol = Comment, nestingLevel--;
615 } while(nestingLevel || symbol == Nothing);
616 yy_text = buf[0] ? buf : (char*)yytext;
621 yylexstr (int boardIndex, char *s, char *buf, int buflen)
624 char *savPP = parsePtr;
626 yyboardindex = boardIndex;
628 ret = (ChessMove) Myylex();
629 strncpy(buf, yy_text, buflen-1);
630 buf[buflen-1] = NULLCHAR;