9 * parser.l -- lex parser of algebraic chess moves for XBoard
11 * Copyright 1991 by Digital Equipment Corporation, Maynard,
14 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005,
15 * 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
17 * The following terms apply to Digital Equipment Corporation's copyright
19 * ------------------------------------------------------------------------
22 * Permission to use, copy, modify, and distribute this software and its
23 * documentation for any purpose and without fee is hereby granted,
24 * provided that the above copyright notice appear in all copies and that
25 * both that copyright notice and this permission notice appear in
26 * supporting documentation, and that the name of Digital not be
27 * used in advertising or publicity pertaining to distribution of the
28 * software without specific, written prior permission.
30 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
31 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
32 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
33 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
34 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
35 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37 * ------------------------------------------------------------------------
39 * The following terms apply to the enhanced version of XBoard
40 * distributed by the Free Software Foundation:
41 * ------------------------------------------------------------------------
43 * GNU XBoard is free software: you can redistribute it and/or modify
44 * it under the terms of the GNU General Public License as published by
45 * the Free Software Foundation, either version 3 of the License, or (at
46 * your option) any later version.
48 * GNU XBoard is distributed in the hope that it will be useful, but
49 * WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51 * General Public License for more details.
53 * You should have received a copy of the GNU General Public License
54 * along with this program. If not, see http://www.gnu.org/licenses/.
56 *------------------------------------------------------------------------
57 ** See the file ChangeLog for a revision history. */
59 /* This parser handles all forms of promotion.
60 * The parser resolves ambiguous moves by searching and check-testing.
61 * It also parses comments of the form [anything] or (anything).
63 * [HGM] Parser extensively modified for bigger boards, Shogi-like syntax,
64 * and unknow pieces. All pieces are now mandatory upper case, but can be
65 * any letter A-Z. Files must be lower case (as before), but can run upto 'l'.
66 * Ranks can be 0-9. The parser returns 0 for off-board files and ranks.
67 * For an unknown piece (as mover or promotion piece) it returns
68 * IllegalMove, like it does when the piece doesn't match.
69 * Promotions can now also be appended Shogi-style, a bare '=' or '+',
70 * and this is then returned as promotion character. The piece indicator
71 * can be prefixed by a '+' to indicate it is a promoted piece.
76 #define NO_CONSTRAINT -1
79 #define UNPUT_BUF_SIZE YYLMAX
82 /* yytext is probably a char*, but could be a char[]. yy_text is set
83 in YY_DECL below, because if yytext is a char*, its value is not
86 #else /*!FLEX_SCANNER*/
87 /* yytext is definitely a char[], so yy_text can be set here, statically. */
88 char *yy_text = (char *) yytext;
93 /* [AP] use prototypes in function declarations */
97 #define YY_PROTO(proto) proto
99 #define YY_PROTO(proto) ()
101 /* end of [AP] fix */
104 #define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size)
107 int _yylex YY_PROTO((void)); \
108 int yylex YY_PROTO((void)) \
110 int result = _yylex(); \
111 yy_text = (char *) yytext; \
114 int _yylex YY_PROTO((void))
122 /* The includes must be here, below the #undef input */
129 #else /* not STDC_HEADERS */
132 # else /* not HAVE_STRING_H */
133 # include <strings.h>
134 # endif /* not HAVE_STRING_H */
135 #endif /* not STDC_HEADERS */
141 #if defined(_amigados)
144 # include <fcntl.h> /* isatty() prototype */
145 # endif /* HAVE_FCNTL_H */
146 #endif /* defined(_amigados) */
150 #include "frontend.h"
154 extern int PosFlags P((int));
156 extern Board boards[MAX_MOVES];
158 int yyskipmoves = FALSE;
159 char currentMoveString[YYLMAX];
161 char unputBuffer[UNPUT_BUF_SIZE];
166 void my_yy_input P((char *buf, int *result, int max_size));
167 #else /*!FLEX_SCANNER*/
168 static int input P((void));
169 static void output P((int ch));
170 static void unput P((int ch));
171 int yylook P((void));
172 int yyback P((int *, int));
175 int yywrap P((void));
176 extern void CopyBoard P((Board to, Board from));
181 "+"?[A-Z][/]?[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|[=+])? {
183 * Fully-qualified algebraic move, possibly with promotion
185 int skip1 = 0, skip2 = 0, skip3 = 0, promoted = 0;
190 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
192 if (yytext[0] == '+') skip1 = skip3 = promoted = 1; /* [HGM] Shogi promoted */
195 if (yytext[1+skip1] == '/') skip1++;
197 /* remove the [xX:-] */
198 if ((yytext[3+skip1] == 'x') || (yytext[3+skip1] == 'X') ||
199 (yytext[3+skip1] == '-') || (yytext[3+skip1] == ':')) skip2 = 1;
201 currentMoveString[0] = yytext[1+skip1];
202 currentMoveString[1] = yytext[2+skip1];
203 currentMoveString[2] = yytext[3+skip1+skip2];
204 currentMoveString[3] = yytext[4+skip1+skip2];
205 currentMoveString[4] = NULLCHAR;
207 if (appData.debugMode) {
208 fprintf(debugFP, "Parser Qa1b2: yyleng=%d\n",
212 if (yyleng-skip1-skip2 > 5) { char c;
213 if (yytext[yyleng-1] == ')') {
214 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
216 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
218 if(c == '+' && gameInfo.variant != VariantShogi) c = currentMoveString[4] = NULLCHAR; // + means check outside Shogi
219 currentMoveString[5] = NULLCHAR;
222 if (appData.debugMode) {
223 fprintf(debugFP, "parser: %s\n", currentMoveString);
225 /* [HGM] do not allow values beyond board size */
226 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
227 currentMoveString[1] - ONE < 0 ||
228 currentMoveString[0] - AAA >= BOARD_RGHT ||
229 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
230 currentMoveString[3] - ONE < 0 ||
231 currentMoveString[2] - AAA >= BOARD_RGHT ||
232 currentMoveString[0] - AAA < BOARD_LEFT ||
233 currentMoveString[2] - AAA < BOARD_LEFT )
234 return ImpossibleMove;
236 piece = boards[yyboardindex]
237 [currentMoveString[1] - ONE][currentMoveString[0] - AAA];
238 if(PieceToChar(piece) == '+' && appData.icsActive) promoted = 1, yytext[skip3] = PieceToChar(DEMOTED piece); // trust ICS
239 if(promoted) piece = (ChessSquare) (DEMOTED piece);
240 c = PieceToChar(piece);
241 if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));
242 if (ToLower(yytext[skip3]) != ToLower(c))
243 return (int) IllegalMove;
245 result = LegalityTest(boards[yyboardindex],
246 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
247 currentMoveString[1] - ONE,
248 currentMoveString[0] - AAA,
249 currentMoveString[3] - ONE,
250 currentMoveString[2] - AAA,
251 currentMoveString[4]);
253 if (currentMoveString[4] == NULLCHAR) {
254 if(result == WhitePromotion || result == BlackPromotion) {
255 if(gameInfo.variant == VariantCourier || gameInfo.variant == VariantShatranj)
256 currentMoveString[4] = PieceToChar(BlackFerz);
257 else if(gameInfo.variant == VariantGreat)
258 currentMoveString[4] = PieceToChar(BlackMan);
259 else if(gameInfo.variant == VariantShogi)
260 currentMoveString[4] = '+';
262 currentMoveString[4] = PieceToChar(BlackQueen);
263 } else if(result == WhiteNonPromotion || result == BlackNonPromotion)
264 currentMoveString[4] = '=';
265 currentMoveString[5] = NULLCHAR;
271 [a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|[=+])? {
273 * Simple algebraic move, possibly with promotion
274 * [HGM] Engine moves are received in this format, with lower-case promoChar!
279 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
281 /* remove the [xX:-] */
282 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
283 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
285 currentMoveString[0] = yytext[0];
286 currentMoveString[1] = yytext[1];
287 currentMoveString[2] = yytext[2+skip];
288 currentMoveString[3] = yytext[3+skip];
289 currentMoveString[4] = NULLCHAR;
291 if (yyleng-skip > 4) { char c;
292 if (yytext[yyleng-1] == ')') {
293 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
295 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
297 if(c == '+' && gameInfo.variant != VariantShogi) c = currentMoveString[4] = NULLCHAR; // + means check outside Shogi
298 currentMoveString[5] = NULLCHAR;
301 /* [HGM] do not allow values beyond board size */
302 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
303 currentMoveString[1] - ONE < 0 ||
304 currentMoveString[0] - AAA >= BOARD_RGHT ||
305 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
306 currentMoveString[3] - ONE < 0 ||
307 currentMoveString[2] - AAA >= BOARD_RGHT ||
308 currentMoveString[0] - AAA < BOARD_LEFT ||
309 currentMoveString[2] - AAA < BOARD_LEFT )
310 return ImpossibleMove;
312 result = LegalityTest(boards[yyboardindex],
313 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
314 currentMoveString[1] - ONE,
315 currentMoveString[0] - AAA,
316 currentMoveString[3] - ONE,
317 currentMoveString[2] - AAA,
318 currentMoveString[4]);
320 if (currentMoveString[4] == NULLCHAR) {
321 if(result == WhitePromotion || result == BlackPromotion) {
322 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
323 currentMoveString[4] = PieceToChar(BlackFerz);
324 else if(gameInfo.variant == VariantGreat)
325 currentMoveString[4] = PieceToChar(BlackMan);
326 else if(gameInfo.variant == VariantShogi)
327 currentMoveString[4] = '+'; // Queen might not be defined in mini variants!
329 currentMoveString[4] = PieceToChar(BlackQueen);
330 } else if(result == WhiteNonPromotion || result == BlackNonPromotion)
331 currentMoveString[4] = '=';
332 currentMoveString[5] = NULLCHAR;
333 } else if(appData.testLegality && // strip off unnecessary and false promo characters
334 !(result == WhitePromotion || result == BlackPromotion ||
335 result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
340 [A-L][0-9][xX:-]?[A-L][0-9] {
342 * Simple algebraic move, in capitals
343 * [HGM] Some Xiangqi engines use this format ('ICCS notation'). So no promotions!
348 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
350 /* remove the [xX:-] */
351 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
352 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
354 currentMoveString[0] = yytext[0]+32;
355 currentMoveString[1] = yytext[1];
356 currentMoveString[2] = yytext[2+skip]+32;
357 currentMoveString[3] = yytext[3+skip];
358 currentMoveString[4] = NULLCHAR;
360 /* [HGM] do not allow values beyond board size */
361 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
362 currentMoveString[1] - ONE < 0 ||
363 currentMoveString[0] - AAA >= BOARD_RGHT ||
364 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
365 currentMoveString[3] - ONE < 0 ||
366 currentMoveString[2] - AAA >= BOARD_RGHT ||
367 currentMoveString[0] - AAA < BOARD_LEFT ||
368 currentMoveString[2] - AAA < BOARD_LEFT )
369 return ImpossibleMove;
371 result = LegalityTest(boards[yyboardindex],
372 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
373 currentMoveString[1] - ONE,
374 currentMoveString[0] - AAA,
375 currentMoveString[3] - ONE,
376 currentMoveString[2] - AAA,
377 currentMoveString[4]);
382 [a-l][0-9]((=?\(?[A-Za-z]\)?)|[=+])? {
384 * Pawn move, possibly with promotion
386 DisambiguateClosure cl;
387 int skip = 0; char c;
389 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
392 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
393 if (yytext[2+skip] == '(') skip++;
395 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
397 cl.ffIn = yytext[0] - AAA;
398 cl.rtIn = yytext[1] - ONE;
399 cl.ftIn = yytext[0] - AAA;
400 cl.promoCharIn = ToLower(yytext[2+skip]);
401 if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
403 /* [HGM] do not allow values beyond board size */
404 if(cl.rtIn >= BOARD_HEIGHT ||
406 cl.ffIn >= BOARD_RGHT ||
407 cl.ftIn < BOARD_LEFT )
408 return ImpossibleMove;
410 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
412 currentMoveString[0] = cl.ff + AAA;
413 currentMoveString[1] = cl.rf + ONE;
414 currentMoveString[2] = cl.ft + AAA;
415 currentMoveString[3] = cl.rt + ONE;
416 currentMoveString[4] = cl.promoChar;
417 currentMoveString[5] = NULLCHAR;
419 return (int) cl.kind;
423 (ab|bc|cd|de|ef|fg|gh|hi|ij|jk|kl|lk|kj|ji|ih|hg|gf|fe|ed|dc|cb|ba|aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|([a-l][xX:-][a-l]))((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
425 * Pawn capture, possibly with promotion, possibly ambiguous
427 DisambiguateClosure cl;
428 int skip1 = 0, skip2 = 0; char c;
430 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
432 /* remove trailing ep or e.p. (nonstandard PGN) */
433 if (yytext[yyleng-1] == 'p') {
435 yytext[yyleng] = NULLCHAR;
436 } else if (yytext[yyleng-1] == '.') {
438 yytext[yyleng] = NULLCHAR;
441 /* remove the [xX:-] and =() */
442 if ((yytext[1] == 'x') || (yytext[1] == 'X')
443 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
444 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
445 if (yytext[2+skip1+skip2] == '(') skip2++;
447 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
449 cl.ffIn = yytext[0] - AAA;
451 cl.ftIn = yytext[1+skip1] - AAA;
452 cl.promoCharIn = yytext[2+skip1+skip2];
453 if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
455 /* [HGM] do not allow values beyond board size */
456 if(cl.ffIn >= BOARD_RGHT ||
457 cl.ffIn < BOARD_LEFT ||
458 cl.ftIn >= BOARD_RGHT ||
459 cl.ftIn < BOARD_LEFT )
460 return ImpossibleMove;
462 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
464 currentMoveString[0] = cl.ff + AAA;
465 currentMoveString[1] = cl.rf + ONE;
466 currentMoveString[2] = cl.ft + AAA;
467 currentMoveString[3] = cl.rt + ONE;
468 currentMoveString[4] = cl.promoChar;
469 currentMoveString[5] = NULLCHAR;
471 return (int) cl.kind;
474 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|[=+])? {
476 * unambiguously abbreviated Pawn capture, possibly with promotion
479 ChessMove result; char c;
481 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
483 /* remove trailing ep or e.p. (nonstandard PGN) */
484 if (yytext[yyleng-1] == 'p') {
486 yytext[yyleng] = NULLCHAR;
487 } else if (yytext[yyleng-1] == '.') {
489 yytext[yyleng] = NULLCHAR;
492 /* remove the [xX:-] */
493 if ((yytext[1] == 'x') || (yytext[1] == 'X')
494 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
496 currentMoveString[0] = yytext[0];
497 currentMoveString[2] = yytext[1+skip];
498 currentMoveString[3] = yytext[2+skip];
500 /* [HGM] do not allow values beyond board size */
501 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
502 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
503 currentMoveString[3] - ONE < 0 ||
504 currentMoveString[2] - AAA >= BOARD_RGHT ||
505 currentMoveString[0] - AAA < BOARD_LEFT ||
506 currentMoveString[2] - AAA < BOARD_LEFT )
507 return ImpossibleMove;
509 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
510 currentMoveString[0] != currentMoveString[2] ) {
511 currentMoveString[1] = yytext[2+skip];
513 if (WhiteOnMove(yyboardindex)) {
514 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
515 currentMoveString[1] = yytext[2+skip] - 1;
516 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn)
517 return ImpossibleMove;
519 currentMoveString[1] = currentMoveString[3] + 1;
520 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
521 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn)
522 return ImpossibleMove;
524 if (yyleng-skip > 3) {
525 if (yytext[yyleng-1] == ')')
526 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
528 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
529 currentMoveString[5] = NULLCHAR;
530 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
531 return ImpossibleMove;
532 if(c == '+' && gameInfo.variant != VariantShogi) c = currentMoveString[4] = NULLCHAR; // + means check outside Shogi
534 currentMoveString[4] = NULLCHAR;
537 result = LegalityTest(boards[yyboardindex],
538 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
539 currentMoveString[1] - ONE,
540 currentMoveString[0] - AAA,
541 currentMoveString[3] - ONE,
542 currentMoveString[2] - AAA,
543 currentMoveString[4]);
545 if (currentMoveString[4] == NULLCHAR) {
546 if(result == WhitePromotion || result == BlackPromotion) {
547 currentMoveString[4] = PieceToChar(BlackQueen);
548 // [HGM] shatranj: take care of variants without Queen
549 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
550 currentMoveString[4] = PieceToChar(BlackFerz);
551 if(gameInfo.variant == VariantGreat)
552 currentMoveString[4] = PieceToChar(BlackMan);
553 if(gameInfo.variant == VariantShogi)
554 currentMoveString[4] = '+';
555 } else if(result == WhiteNonPromotion || result == BlackNonPromotion)
556 currentMoveString[4] = '=';
557 currentMoveString[5] = NULLCHAR;
560 if (result != IllegalMove) return (int) result;
562 /* Special case: improperly written en passant capture */
563 if (WhiteOnMove(yyboardindex)) {
564 if (currentMoveString[3] == '5') {
565 currentMoveString[1] = '5';
566 currentMoveString[3] = '6';
568 return (int) IllegalMove;
571 if (currentMoveString[3] == '4') {
572 currentMoveString[1] = '4';
573 currentMoveString[3] = '3';
575 return (int) IllegalMove;
579 result = LegalityTest(boards[yyboardindex],
580 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
581 currentMoveString[1] - ONE,
582 currentMoveString[0] - AAA,
583 currentMoveString[3] - ONE,
584 currentMoveString[2] - AAA,
585 currentMoveString[4]);
587 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
590 return (int) IllegalMove;
593 "+"?[A-Z][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|[=+])? {
595 * piece move, possibly ambiguous
597 DisambiguateClosure cl;
598 int skip = 0, skip2 = 0, promoted = 0;
600 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
602 if(yytext[0] == '+') promoted = skip = skip2 = 1;
604 /* remove the [xX:-] */
605 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
606 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
608 if (WhiteOnMove(yyboardindex)) {
609 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
611 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
613 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
617 cl.rtIn = yytext[2+skip] - ONE;
618 cl.ftIn = yytext[1+skip] - AAA;
619 cl.promoCharIn = NULLCHAR;
621 if(yyleng-skip > 3 && gameInfo.variant == VariantShogi) /* [HGM] can have Shogi-style promotion */
622 cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
623 if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
625 if (appData.debugMode) {
626 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
628 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
631 /* [HGM] but do not allow values beyond board size */
632 if(cl.rtIn >= BOARD_HEIGHT ||
634 cl.ftIn >= BOARD_RGHT ||
635 cl.ftIn < BOARD_LEFT )
636 return ImpossibleMove;
638 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
640 currentMoveString[0] = cl.ff + AAA;
641 currentMoveString[1] = cl.rf + ONE;
642 currentMoveString[2] = cl.ft + AAA;
643 currentMoveString[3] = cl.rt + ONE;
644 currentMoveString[4] = cl.promoChar;
645 currentMoveString[5] = NULLCHAR;
647 return (int) cl.kind;
650 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|[=+])? {
652 * piece move with rank or file disambiguator
654 DisambiguateClosure cl;
655 int skip = 0, skip2 = 0; int promoted=0;
657 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
659 if(yytext[0]=='+') promoted = skip = skip2 = 1;
661 /* remove the [xX:-] */
662 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
663 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
665 if (WhiteOnMove(yyboardindex)) {
666 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
668 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
670 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
672 if (isalpha(yytext[1+skip2])) {
674 cl.ffIn = yytext[1+skip2] - AAA;
676 if(cl.ffIn >= BOARD_RGHT ||
677 cl.ffIn < BOARD_LEFT ) return 0;
679 cl.rfIn = yytext[1+skip2] - ONE;
681 if(cl.rfIn >= BOARD_HEIGHT ||
682 cl.rfIn < 0) return 0;
684 cl.rtIn = yytext[3+skip] - ONE;
685 cl.ftIn = yytext[2+skip] - AAA;
686 cl.promoCharIn = NULLCHAR;
688 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
689 cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
690 if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
692 /* [HGM] do not allow values beyond board size */
693 if(cl.rtIn >= BOARD_HEIGHT ||
695 cl.ftIn >= BOARD_RGHT ||
696 cl.ftIn < BOARD_LEFT )
697 return ImpossibleMove;
699 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
701 currentMoveString[0] = cl.ff + AAA;
702 currentMoveString[1] = cl.rf + ONE;
703 currentMoveString[2] = cl.ft + AAA;
704 currentMoveString[3] = cl.rt + ONE;
705 currentMoveString[4] = cl.promoChar;
706 currentMoveString[5] = NULLCHAR;
708 return (int) cl.kind;
711 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
714 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
716 /* [HGM] all squares referenced to board edges in stead of absolute */
717 if (WhiteOnMove(yyboardindex)) {
718 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
719 /* ICS wild castling */
721 ff = (BOARD_WIDTH-1)>>1;
731 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
732 /* ICS wild castling */
734 ff = (BOARD_WIDTH-1)>>1;
744 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
746 if (WhiteOnMove(yyboardindex)) {
747 ff = initialRights[2];
748 ft = initialRights[1];
750 ff = initialRights[5];
751 ft = initialRights[4];
753 if (appData.debugMode)
755 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
757 if(ff < 0 || ft < 0) return 0;
759 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
760 if (appData.debugMode) {
761 fprintf(debugFP, "long castling %d %d\n", ff, ft);
763 return (int) LegalityTest(boards[yyboardindex],
764 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
765 rf, ff, rt, ft, NULLCHAR);
768 00|0-0|oo|OO|o-o|O-O {
771 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
773 if (WhiteOnMove(yyboardindex)) {
774 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
775 /* ICS wild castling */
777 ff = (BOARD_WIDTH-1)>>1;
787 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
788 /* ICS wild castling */
790 ff = (BOARD_WIDTH-1)>>1;
800 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
801 if (WhiteOnMove(yyboardindex)) {
802 ff = initialRights[2];
803 ft = initialRights[0];
805 ff = initialRights[5];
806 ft = initialRights[3];
808 if (appData.debugMode) {
809 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
811 if(ff < 0 || ft < 0) return 0;
813 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
814 if (appData.debugMode) {
815 fprintf(debugFP, "short castling %d %d\n", ff, ft);
818 return (int) LegalityTest(boards[yyboardindex],
819 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
820 rf, ff, rt, ft, NULLCHAR);
823 [A-Za-z][@*][a-l][0-9] {
824 /* Bughouse piece drop. */
825 currentMoveString[1] = '@';
826 currentMoveString[2] = yytext[2];
827 currentMoveString[3] = yytext[3];
828 currentMoveString[4] = NULLCHAR;
830 if (appData.debugMode) {
831 fprintf(debugFP, "Drop: %s\n", currentMoveString);
833 /* [HGM] do not allow values beyond board size */
834 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
835 currentMoveString[2] - AAA >= BOARD_WIDTH )
836 return ImpossibleMove;
838 if (WhiteOnMove(yyboardindex)) {
839 currentMoveString[0] = ToUpper(yytext[0]);
841 currentMoveString[0] = ToLower(yytext[0]);
843 return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), DROP_RANK, // [HGM] does drops now too
844 CharToPiece(currentMoveString[0]), currentMoveString[3] - ONE, currentMoveString[2] - AAA, NULLCHAR);
848 if (WhiteOnMove(yyboardindex))
849 return (int) BlackWins;
851 return (int) WhiteWins;
854 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
855 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
858 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
859 return (int) GameUnfinished;
863 return (int) GameIsDrawn;
867 return (int) GameIsDrawn;
871 if (WhiteOnMove(yyboardindex))
872 return (int) BlackWins;
874 return (int) WhiteWins;
878 if (WhiteOnMove(yyboardindex))
879 return (int) BlackWins;
881 return (int) WhiteWins;
884 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
885 return (int) GameIsDrawn;
888 [Dd]raw(n)?(" (".*")")? {
889 return (int) GameIsDrawn;
892 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) {
893 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
896 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) {
897 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
900 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
901 return (int) WhiteWins;
904 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
905 return (int) BlackWins;
908 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
909 return (int) GameIsDrawn;
912 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
913 return (int) GameUnfinished;
916 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
918 if ((yyleng == 1) && (yytext[0] == '1'))
919 return (int) MoveNumberOne;
922 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
923 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
924 return (int) ElapsedTime;
928 /* position diagram enclosed in [-- --] */
929 return (int) PositionDiagram;
932 ^"{--------------"\n[^\}]*\n"--------------}"$ {
933 /* position diagram enclosed in {-- --} */
934 return (int) PositionDiagram;
937 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
941 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
942 return (int) GNUChessGame;
945 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
946 return (int) XBoardGame;
949 \$[0-9]+ { /* numeric annotation glyph */
953 \{[^\}]*\} { /* anything in {} */
954 return (int) Comment;
957 ;.*$ { /* ; to end of line */
958 return (int) Comment;
961 \[[^\]]*\] { /* anything in [] */
962 return (int) Comment;
965 \([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */
966 return (int) Comment;
969 \([^)][^)]+\) { /* >=2 chars in () */
970 return (int) Comment;
973 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
974 /* Skip mail headers */
978 /* Skip random words */
982 /* Skip everything else */
988 static char *StringToLex;
997 if (StringToLex != NULL) {
1003 } else if (unputCount > 0) {
1004 ret = unputBuffer[--unputCount];
1016 * Return offset of next pattern within current file
1020 int offset = ftell(lexFP) - unputCount;
1028 static void output(ch)
1031 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",
1035 static void unput(ch)
1038 if (ch == 0) return;
1039 if (StringToLex != NULL) {
1042 if (unputCount >= UNPUT_BUF_SIZE)
1043 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
1045 unputBuffer[unputCount++] = ch;
1049 /* Get ready to lex from a new file. Kludge below sticks
1050 an artificial newline at the front of the file, which the
1051 above grammar ignores, but which makes ^ at start of pattern
1052 match at the real start of the file.
1060 unput('\n'); /* kludge */
1063 /* Get ready to lex from a string. ^ at start of pattern WON'T
1064 match at the start of the string!
1073 #endif /*!FLEX_SCANNER*/
1076 void my_yy_input(buf, result, max_size)
1083 if (StringToLex != NULL) {
1085 while (*StringToLex != NULLCHAR) {
1086 *buf++ = *StringToLex++;
1092 count = fread(buf, 1, max_size, yyin);
1102 static YY_BUFFER_STATE my_file_buffer = NULL;
1105 Return offset of next pattern in the current file.
1109 int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
1111 return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
1119 if (my_file_buffer != NULL)
1120 yy_delete_buffer(my_file_buffer);
1122 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1123 yy_switch_to_buffer(my_file_buffer);
1129 if (my_file_buffer != NULL)
1130 yy_delete_buffer(my_file_buffer);
1132 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
1133 yy_switch_to_buffer(my_file_buffer);
1135 #endif /*FLEX_SCANNER*/
1142 /* Parse a move from the given string s */
1143 /* ^ at start of pattern WON'T work here unless using flex */
1144 ChessMove yylexstr(boardIndex, s, text, len)
1145 int boardIndex, len;
1149 char *oldStringToLex;
1151 YY_BUFFER_STATE buffer, oldBuffer;
1154 yyboardindex = boardIndex;
1155 oldStringToLex = StringToLex;
1158 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1159 oldBuffer = YY_CURRENT_BUFFER;
1160 yy_switch_to_buffer(buffer);
1161 #endif /*FLEX_SCANNER*/
1163 ret = (ChessMove) yylex();
1164 strncpy(text, yy_text, len-1); // [HGM] vari: yy_text is not available to caller after buffer switch ?!?
1165 text[len-1] = NULLCHAR;
1168 if (oldBuffer != NULL)
1169 yy_switch_to_buffer(oldBuffer);
1170 yy_delete_buffer(buffer);
1171 #endif /*FLEX_SCANNER*/
1172 StringToLex = oldStringToLex;