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 currentMoveString[5] = NULLCHAR;
219 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
220 return IllegalMove; /* [HGM] promotion to invalid piece */
223 if (appData.debugMode) {
224 fprintf(debugFP, "parser: %s\n", currentMoveString);
226 /* [HGM] do not allow values beyond board size */
227 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
228 currentMoveString[1] - ONE < 0 ||
229 currentMoveString[0] - AAA >= BOARD_RGHT ||
230 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
231 currentMoveString[3] - ONE < 0 ||
232 currentMoveString[2] - AAA >= BOARD_RGHT ||
233 currentMoveString[0] - AAA < BOARD_LEFT ||
234 currentMoveString[2] - AAA < BOARD_LEFT )
235 return ImpossibleMove;
237 piece = boards[yyboardindex]
238 [currentMoveString[1] - ONE][currentMoveString[0] - AAA];
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 (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 currentMoveString[5] = NULLCHAR;
269 [a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
271 * Simple algebraic move, possibly with promotion
272 * [HGM] Engine moves are received in this format, with lower-case promoChar!
277 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
279 /* remove the [xX:-] */
280 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
281 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
283 currentMoveString[0] = yytext[0];
284 currentMoveString[1] = yytext[1];
285 currentMoveString[2] = yytext[2+skip];
286 currentMoveString[3] = yytext[3+skip];
287 currentMoveString[4] = NULLCHAR;
289 if (yyleng-skip > 4) { char c;
290 if (yytext[yyleng-1] == ')') {
291 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
293 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
295 currentMoveString[5] = NULLCHAR;
296 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
300 /* [HGM] do not allow values beyond board size */
301 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
302 currentMoveString[1] - ONE < 0 ||
303 currentMoveString[0] - AAA >= BOARD_RGHT ||
304 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
305 currentMoveString[3] - ONE < 0 ||
306 currentMoveString[2] - AAA >= BOARD_RGHT ||
307 currentMoveString[0] - AAA < BOARD_LEFT ||
308 currentMoveString[2] - AAA < BOARD_LEFT )
309 return ImpossibleMove;
311 result = LegalityTest(boards[yyboardindex],
312 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
313 currentMoveString[1] - ONE,
314 currentMoveString[0] - AAA,
315 currentMoveString[3] - ONE,
316 currentMoveString[2] - AAA,
317 currentMoveString[4]);
319 if (currentMoveString[4] == NULLCHAR) {
320 if(result == WhitePromotion || result == BlackPromotion) {
321 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
322 currentMoveString[4] = PieceToChar(BlackFerz);
323 else if(gameInfo.variant == VariantGreat)
324 currentMoveString[4] = PieceToChar(BlackMan);
325 else if(gameInfo.variant == VariantShogi)
326 currentMoveString[4] = '+'; // Queen might not be defined in mini variants!
328 currentMoveString[4] = PieceToChar(BlackQueen);
329 currentMoveString[5] = NULLCHAR;
331 } else if(appData.testLegality && // strip off unnecessary and false promo characters
332 !(result == WhitePromotion || result == BlackPromotion ||
333 result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
338 [A-L][0-9][xX:-]?[A-L][0-9] {
340 * Simple algebraic move, in capitals
341 * [HGM] Engine moves are received in this format, with lower-case promoChar!
346 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
348 /* remove the [xX:-] */
349 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
350 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
352 currentMoveString[0] = yytext[0]+32;
353 currentMoveString[1] = yytext[1];
354 currentMoveString[2] = yytext[2+skip]+32;
355 currentMoveString[3] = yytext[3+skip];
356 currentMoveString[4] = NULLCHAR;
358 /* [HGM] do not allow values beyond board size */
359 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
360 currentMoveString[1] - ONE < 0 ||
361 currentMoveString[0] - AAA >= BOARD_RGHT ||
362 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
363 currentMoveString[3] - ONE < 0 ||
364 currentMoveString[2] - AAA >= BOARD_RGHT ||
365 currentMoveString[0] - AAA < BOARD_LEFT ||
366 currentMoveString[2] - AAA < BOARD_LEFT )
367 return ImpossibleMove;
369 result = LegalityTest(boards[yyboardindex],
370 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
371 currentMoveString[1] - ONE,
372 currentMoveString[0] - AAA,
373 currentMoveString[3] - ONE,
374 currentMoveString[2] - AAA,
375 currentMoveString[4]);
377 if (currentMoveString[4] == NULLCHAR &&
378 (result == WhitePromotion || result == BlackPromotion)) {
379 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
380 currentMoveString[4] = PieceToChar(BlackFerz);
381 else if(gameInfo.variant == VariantGreat)
382 currentMoveString[4] = PieceToChar(BlackMan);
384 currentMoveString[4] = PieceToChar(BlackQueen);
385 currentMoveString[5] = NULLCHAR;
391 [a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
393 * Pawn move, possibly with promotion
395 DisambiguateClosure cl;
396 int skip = 0; char c;
398 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
401 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
402 if (yytext[2+skip] == '(') skip++;
404 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
406 cl.ffIn = yytext[0] - AAA;
407 cl.rtIn = yytext[1] - ONE;
408 cl.ftIn = yytext[0] - AAA;
409 c = cl.promoCharIn = ToLower(yytext[2+skip]);
411 /* [HGM] do not allow values beyond board size */
412 if(cl.rtIn >= BOARD_HEIGHT ||
414 cl.ffIn >= BOARD_RGHT ||
415 cl.ftIn < BOARD_LEFT )
416 return ImpossibleMove;
418 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
422 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
424 currentMoveString[0] = cl.ff + AAA;
425 currentMoveString[1] = cl.rf + ONE;
426 currentMoveString[2] = cl.ft + AAA;
427 currentMoveString[3] = cl.rt + ONE;
428 currentMoveString[4] = cl.promoChar;
429 currentMoveString[5] = NULLCHAR;
431 return (int) cl.kind;
435 (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."|=)? {
437 * Pawn capture, possibly with promotion, possibly ambiguous
439 DisambiguateClosure cl;
440 int skip1 = 0, skip2 = 0; char c;
442 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
444 /* remove trailing ep or e.p. (nonstandard PGN) */
445 if (yytext[yyleng-1] == 'p') {
447 yytext[yyleng] = NULLCHAR;
448 } else if (yytext[yyleng-1] == '.') {
450 yytext[yyleng] = NULLCHAR;
453 /* remove the [xX:-] and =() */
454 if ((yytext[1] == 'x') || (yytext[1] == 'X')
455 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
456 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
457 if (yytext[2+skip1+skip2] == '(') skip2++;
459 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
461 cl.ffIn = yytext[0] - AAA;
463 cl.ftIn = yytext[1+skip1] - AAA;
464 c = cl.promoCharIn = yytext[2+skip1+skip2];
466 /* [HGM] do not allow values beyond board size */
467 if(cl.ffIn >= BOARD_RGHT ||
468 cl.ffIn < BOARD_LEFT ||
469 cl.ftIn >= BOARD_RGHT ||
470 cl.ftIn < BOARD_LEFT )
471 return ImpossibleMove;
473 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
476 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
478 currentMoveString[0] = cl.ff + AAA;
479 currentMoveString[1] = cl.rf + ONE;
480 currentMoveString[2] = cl.ft + AAA;
481 currentMoveString[3] = cl.rt + ONE;
482 currentMoveString[4] = cl.promoChar;
483 currentMoveString[5] = NULLCHAR;
485 return (int) cl.kind;
488 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
490 * unambiguously abbreviated Pawn capture, possibly with promotion
493 ChessMove result; char c;
495 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
497 /* remove trailing ep or e.p. (nonstandard PGN) */
498 if (yytext[yyleng-1] == 'p') {
500 yytext[yyleng] = NULLCHAR;
501 } else if (yytext[yyleng-1] == '.') {
503 yytext[yyleng] = NULLCHAR;
506 /* remove the [xX:-] */
507 if ((yytext[1] == 'x') || (yytext[1] == 'X')
508 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
510 currentMoveString[0] = yytext[0];
511 currentMoveString[2] = yytext[1+skip];
512 currentMoveString[3] = yytext[2+skip];
514 /* [HGM] do not allow values beyond board size */
515 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
516 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
517 currentMoveString[3] - ONE < 0 ||
518 currentMoveString[2] - AAA >= BOARD_RGHT ||
519 currentMoveString[0] - AAA < BOARD_LEFT ||
520 currentMoveString[2] - AAA < BOARD_LEFT )
521 return ImpossibleMove;
523 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
524 currentMoveString[0] != currentMoveString[2] ) {
525 currentMoveString[1] = yytext[2+skip];
527 if (WhiteOnMove(yyboardindex)) {
528 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
529 currentMoveString[1] = yytext[2+skip] - 1;
530 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn)
531 return ImpossibleMove;
533 currentMoveString[1] = currentMoveString[3] + 1;
534 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
535 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn)
536 return ImpossibleMove;
538 if (yyleng-skip > 3) {
539 if (yytext[yyleng-1] == ')')
540 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
542 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
543 currentMoveString[5] = NULLCHAR;
544 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
547 currentMoveString[4] = NULLCHAR;
550 result = LegalityTest(boards[yyboardindex],
551 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
552 currentMoveString[1] - ONE,
553 currentMoveString[0] - AAA,
554 currentMoveString[3] - ONE,
555 currentMoveString[2] - AAA,
556 currentMoveString[4]);
558 if (currentMoveString[4] == NULLCHAR &&
559 (result == WhitePromotion || result == BlackPromotion)) {
560 currentMoveString[4] = PieceToChar(BlackQueen);
561 // [HGM] shatranj: take care of variants without Queen
562 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
563 currentMoveString[4] = PieceToChar(BlackFerz);
564 if(gameInfo.variant == VariantGreat)
565 currentMoveString[4] = PieceToChar(BlackMan);
566 currentMoveString[5] = NULLCHAR;
569 if (result != IllegalMove) return (int) result;
571 /* Special case: improperly written en passant capture */
572 if (WhiteOnMove(yyboardindex)) {
573 if (currentMoveString[3] == '5') {
574 currentMoveString[1] = '5';
575 currentMoveString[3] = '6';
577 return (int) IllegalMove;
580 if (currentMoveString[3] == '4') {
581 currentMoveString[1] = '4';
582 currentMoveString[3] = '3';
584 return (int) IllegalMove;
588 result = LegalityTest(boards[yyboardindex],
589 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
590 currentMoveString[1] - ONE,
591 currentMoveString[0] - AAA,
592 currentMoveString[3] - ONE,
593 currentMoveString[2] - AAA,
594 currentMoveString[4]);
596 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
599 return (int) IllegalMove;
602 "+"?[A-Z][xX:-]?[a-l][0-9]=? {
604 * piece move, possibly ambiguous
606 DisambiguateClosure cl;
607 int skip = 0, skip2 = 0, promoted = 0;
609 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
611 if(yytext[0] == '+') promoted = skip = skip2 = 1;
613 /* remove the [xX:-] */
614 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
615 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
617 if (WhiteOnMove(yyboardindex)) {
618 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
620 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
622 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
626 cl.rtIn = yytext[2+skip] - ONE;
627 cl.ftIn = yytext[1+skip] - AAA;
628 cl.promoCharIn = NULLCHAR;
630 if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
631 cl.promoCharIn = yytext[yyleng-1];
633 if (appData.debugMode) {
634 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
636 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
639 /* [HGM] but do not allow values beyond board size */
640 if(cl.rtIn >= BOARD_HEIGHT ||
642 cl.ftIn >= BOARD_RGHT ||
643 cl.ftIn < BOARD_LEFT )
644 return ImpossibleMove;
646 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
648 currentMoveString[0] = cl.ff + AAA;
649 currentMoveString[1] = cl.rf + ONE;
650 currentMoveString[2] = cl.ft + AAA;
651 currentMoveString[3] = cl.rt + ONE;
652 currentMoveString[4] = cl.promoChar;
653 currentMoveString[5] = NULLCHAR;
655 return (int) cl.kind;
658 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=? {
660 * piece move with rank or file disambiguator
662 DisambiguateClosure cl;
663 int skip = 0, skip2 = 0; int promoted=0;
665 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
667 if(yytext[0]=='+') promoted = skip = skip2 = 1;
669 /* remove the [xX:-] */
670 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
671 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
673 if (WhiteOnMove(yyboardindex)) {
674 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
676 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
678 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
680 if (isalpha(yytext[1+skip2])) {
682 cl.ffIn = yytext[1+skip2] - AAA;
684 if(cl.ffIn >= BOARD_RGHT ||
685 cl.ffIn < BOARD_LEFT ) return 0;
687 cl.rfIn = yytext[1+skip2] - ONE;
689 if(cl.rfIn >= BOARD_HEIGHT ||
690 cl.rfIn < 0) return 0;
692 cl.rtIn = yytext[3+skip] - ONE;
693 cl.ftIn = yytext[2+skip] - AAA;
694 cl.promoCharIn = NULLCHAR;
696 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
697 cl.promoCharIn = yytext[yyleng-1];
699 /* [HGM] do not allow values beyond board size */
700 if(cl.rtIn >= BOARD_HEIGHT ||
702 cl.ftIn >= BOARD_RGHT ||
703 cl.ftIn < BOARD_LEFT )
704 return ImpossibleMove;
706 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
708 currentMoveString[0] = cl.ff + AAA;
709 currentMoveString[1] = cl.rf + ONE;
710 currentMoveString[2] = cl.ft + AAA;
711 currentMoveString[3] = cl.rt + ONE;
712 currentMoveString[4] = cl.promoChar;
713 currentMoveString[5] = NULLCHAR;
715 return (int) cl.kind;
718 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
721 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
723 /* [HGM] all squares referenced to board edges in stead of absolute */
724 if (WhiteOnMove(yyboardindex)) {
725 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
726 /* ICS wild castling */
728 ff = (BOARD_WIDTH-1)>>1;
738 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
739 /* ICS wild castling */
741 ff = (BOARD_WIDTH-1)>>1;
751 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
753 if (WhiteOnMove(yyboardindex)) {
754 ff = initialRights[2];
755 ft = initialRights[1];
757 ff = initialRights[5];
758 ft = initialRights[4];
760 if (appData.debugMode)
762 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
764 if(ff < 0 || ft < 0) return 0;
766 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
767 if (appData.debugMode) {
768 fprintf(debugFP, "long castling %d %d\n", ff, ft);
770 return (int) LegalityTest(boards[yyboardindex],
771 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
772 rf, ff, rt, ft, NULLCHAR);
775 00|0-0|oo|OO|o-o|O-O {
778 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
780 if (WhiteOnMove(yyboardindex)) {
781 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
782 /* ICS wild castling */
784 ff = (BOARD_WIDTH-1)>>1;
794 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
795 /* ICS wild castling */
797 ff = (BOARD_WIDTH-1)>>1;
807 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
808 if (WhiteOnMove(yyboardindex)) {
809 ff = initialRights[2];
810 ft = initialRights[0];
812 ff = initialRights[5];
813 ft = initialRights[3];
815 if (appData.debugMode) {
816 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
818 if(ff < 0 || ft < 0) return 0;
820 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
821 if (appData.debugMode) {
822 fprintf(debugFP, "short castling %d %d\n", ff, ft);
825 return (int) LegalityTest(boards[yyboardindex],
826 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
827 rf, ff, rt, ft, NULLCHAR);
830 [A-Z][@*][a-l][0-9] {
831 /* Bughouse piece drop. */
832 currentMoveString[1] = '@';
833 currentMoveString[2] = yytext[2];
834 currentMoveString[3] = yytext[3];
835 currentMoveString[4] = NULLCHAR;
837 if (appData.debugMode) {
838 fprintf(debugFP, "Drop: %s\n", currentMoveString);
840 /* [HGM] do not allow values beyond board size */
841 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
842 currentMoveString[2] - AAA >= BOARD_WIDTH )
843 return ImpossibleMove;
845 if (WhiteOnMove(yyboardindex)) {
846 currentMoveString[0] = ToUpper(yytext[0]);
848 currentMoveString[0] = ToLower(yytext[0]);
850 return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), DROP_RANK, // [HGM] does drops now too
851 CharToPiece(currentMoveString[0]), currentMoveString[3] - ONE, currentMoveString[2] - AAA, NULLCHAR);
855 if (WhiteOnMove(yyboardindex))
856 return (int) BlackWins;
858 return (int) WhiteWins;
861 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
862 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
865 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
866 return (int) GameUnfinished;
870 return (int) GameIsDrawn;
874 return (int) GameIsDrawn;
878 if (WhiteOnMove(yyboardindex))
879 return (int) BlackWins;
881 return (int) WhiteWins;
885 if (WhiteOnMove(yyboardindex))
886 return (int) BlackWins;
888 return (int) WhiteWins;
891 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
892 return (int) GameIsDrawn;
895 [Dd]raw(n)?(" (".*")")? {
896 return (int) GameIsDrawn;
899 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) {
900 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
903 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) {
904 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
907 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
908 return (int) WhiteWins;
911 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
912 return (int) BlackWins;
915 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
916 return (int) GameIsDrawn;
919 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
920 return (int) GameUnfinished;
923 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
925 if ((yyleng == 1) && (yytext[0] == '1'))
926 return (int) MoveNumberOne;
929 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
930 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
931 return (int) ElapsedTime;
935 /* position diagram enclosed in [-- --] */
936 return (int) PositionDiagram;
939 ^"{--------------"\n[^\}]*\n"--------------}"$ {
940 /* position diagram enclosed in {-- --} */
941 return (int) PositionDiagram;
944 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
948 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
949 return (int) GNUChessGame;
952 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
953 return (int) XBoardGame;
956 \$[0-9]+ { /* numeric annotation glyph */
960 \{[^\}]*\} { /* anything in {} */
961 return (int) Comment;
964 ;.*$ { /* ; to end of line */
965 return (int) Comment;
968 \[[^\]]*\] { /* anything in [] */
969 return (int) Comment;
972 \([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */
973 return (int) Comment;
976 \([^)][^)]+\) { /* >=2 chars in () */
977 return (int) Comment;
980 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
981 /* Skip mail headers */
985 /* Skip random words */
989 /* Skip everything else */
995 static char *StringToLex;
1004 if (StringToLex != NULL) {
1006 if (ret == NULLCHAR)
1010 } else if (unputCount > 0) {
1011 ret = unputBuffer[--unputCount];
1023 * Return offset of next pattern within current file
1027 int offset = ftell(lexFP) - unputCount;
1035 static void output(ch)
1038 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",
1042 static void unput(ch)
1045 if (ch == 0) return;
1046 if (StringToLex != NULL) {
1049 if (unputCount >= UNPUT_BUF_SIZE)
1050 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
1052 unputBuffer[unputCount++] = ch;
1056 /* Get ready to lex from a new file. Kludge below sticks
1057 an artificial newline at the front of the file, which the
1058 above grammar ignores, but which makes ^ at start of pattern
1059 match at the real start of the file.
1067 unput('\n'); /* kludge */
1070 /* Get ready to lex from a string. ^ at start of pattern WON'T
1071 match at the start of the string!
1080 #endif /*!FLEX_SCANNER*/
1083 void my_yy_input(buf, result, max_size)
1090 if (StringToLex != NULL) {
1092 while (*StringToLex != NULLCHAR) {
1093 *buf++ = *StringToLex++;
1099 count = fread(buf, 1, max_size, yyin);
1109 static YY_BUFFER_STATE my_file_buffer = NULL;
1112 Return offset of next pattern in the current file.
1116 int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
1118 return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
1126 if (my_file_buffer != NULL)
1127 yy_delete_buffer(my_file_buffer);
1129 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1130 yy_switch_to_buffer(my_file_buffer);
1136 if (my_file_buffer != NULL)
1137 yy_delete_buffer(my_file_buffer);
1139 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
1140 yy_switch_to_buffer(my_file_buffer);
1142 #endif /*FLEX_SCANNER*/
1149 /* Parse a move from the given string s */
1150 /* ^ at start of pattern WON'T work here unless using flex */
1151 ChessMove yylexstr(boardIndex, s, text, len)
1152 int boardIndex, len;
1156 char *oldStringToLex;
1158 YY_BUFFER_STATE buffer, oldBuffer;
1161 yyboardindex = boardIndex;
1162 oldStringToLex = StringToLex;
1165 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1166 oldBuffer = YY_CURRENT_BUFFER;
1167 yy_switch_to_buffer(buffer);
1168 #endif /*FLEX_SCANNER*/
1170 ret = (ChessMove) yylex();
1171 strncpy(text, yy_text, len-1); // [HGM] vari: yy_text is not available to caller after buffer switch ?!?
1172 text[len-1] = NULLCHAR;
1175 if (oldBuffer != NULL)
1176 yy_switch_to_buffer(oldBuffer);
1177 yy_delete_buffer(buffer);
1178 #endif /*FLEX_SCANNER*/
1179 StringToLex = oldStringToLex;