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(PieceToChar(piece) == '+' && appData.icsActive) promoted = 1, yytext[skip3] = PieceToChar(DEMOTED piece); // trust ICS
240 if(promoted) piece = (ChessSquare) (DEMOTED piece);
241 c = PieceToChar(piece);
242 if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));
243 if (ToLower(yytext[skip3]) != ToLower(c))
244 return (int) IllegalMove;
246 result = LegalityTest(boards[yyboardindex],
247 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
248 currentMoveString[1] - ONE,
249 currentMoveString[0] - AAA,
250 currentMoveString[3] - ONE,
251 currentMoveString[2] - AAA,
252 currentMoveString[4]);
254 if (currentMoveString[4] == NULLCHAR &&
255 (result == WhitePromotion || result == BlackPromotion)) {
256 if(gameInfo.variant == VariantCourier || gameInfo.variant == VariantShatranj)
257 currentMoveString[4] = PieceToChar(BlackFerz);
258 else if(gameInfo.variant == VariantGreat)
259 currentMoveString[4] = PieceToChar(BlackMan);
260 else if(gameInfo.variant == VariantShogi)
261 currentMoveString[4] = '+';
263 currentMoveString[4] = PieceToChar(BlackQueen);
264 currentMoveString[5] = NULLCHAR;
270 [a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
272 * Simple algebraic move, possibly with promotion
273 * [HGM] Engine moves are received in this format, with lower-case promoChar!
278 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
280 /* remove the [xX:-] */
281 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
282 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
284 currentMoveString[0] = yytext[0];
285 currentMoveString[1] = yytext[1];
286 currentMoveString[2] = yytext[2+skip];
287 currentMoveString[3] = yytext[3+skip];
288 currentMoveString[4] = NULLCHAR;
290 if (yyleng-skip > 4) { char c;
291 if (yytext[yyleng-1] == ')') {
292 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
294 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
296 currentMoveString[5] = NULLCHAR;
297 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
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 currentMoveString[5] = NULLCHAR;
332 } else if(appData.testLegality && // strip off unnecessary and false promo characters
333 !(result == WhitePromotion || result == BlackPromotion ||
334 result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
339 [A-L][0-9][xX:-]?[A-L][0-9] {
341 * Simple algebraic move, in capitals
342 * [HGM] Engine moves are received in this format, with lower-case promoChar!
347 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
349 /* remove the [xX:-] */
350 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
351 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
353 currentMoveString[0] = yytext[0]+32;
354 currentMoveString[1] = yytext[1];
355 currentMoveString[2] = yytext[2+skip]+32;
356 currentMoveString[3] = yytext[3+skip];
357 currentMoveString[4] = NULLCHAR;
359 /* [HGM] do not allow values beyond board size */
360 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
361 currentMoveString[1] - ONE < 0 ||
362 currentMoveString[0] - AAA >= BOARD_RGHT ||
363 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
364 currentMoveString[3] - ONE < 0 ||
365 currentMoveString[2] - AAA >= BOARD_RGHT ||
366 currentMoveString[0] - AAA < BOARD_LEFT ||
367 currentMoveString[2] - AAA < BOARD_LEFT )
368 return ImpossibleMove;
370 result = LegalityTest(boards[yyboardindex],
371 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
372 currentMoveString[1] - ONE,
373 currentMoveString[0] - AAA,
374 currentMoveString[3] - ONE,
375 currentMoveString[2] - AAA,
376 currentMoveString[4]);
378 if (currentMoveString[4] == NULLCHAR &&
379 (result == WhitePromotion || result == BlackPromotion)) {
380 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
381 currentMoveString[4] = PieceToChar(BlackFerz);
382 else if(gameInfo.variant == VariantGreat)
383 currentMoveString[4] = PieceToChar(BlackMan);
385 currentMoveString[4] = PieceToChar(BlackQueen);
386 currentMoveString[5] = NULLCHAR;
392 [a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
394 * Pawn move, possibly with promotion
396 DisambiguateClosure cl;
397 int skip = 0; char c;
399 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
402 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
403 if (yytext[2+skip] == '(') skip++;
405 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
407 cl.ffIn = yytext[0] - AAA;
408 cl.rtIn = yytext[1] - ONE;
409 cl.ftIn = yytext[0] - AAA;
410 c = cl.promoCharIn = ToLower(yytext[2+skip]);
412 /* [HGM] do not allow values beyond board size */
413 if(cl.rtIn >= BOARD_HEIGHT ||
415 cl.ffIn >= BOARD_RGHT ||
416 cl.ftIn < BOARD_LEFT )
417 return ImpossibleMove;
419 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
423 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
425 currentMoveString[0] = cl.ff + AAA;
426 currentMoveString[1] = cl.rf + ONE;
427 currentMoveString[2] = cl.ft + AAA;
428 currentMoveString[3] = cl.rt + ONE;
429 currentMoveString[4] = cl.promoChar;
430 currentMoveString[5] = NULLCHAR;
432 return (int) cl.kind;
436 (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."|=)? {
438 * Pawn capture, possibly with promotion, possibly ambiguous
440 DisambiguateClosure cl;
441 int skip1 = 0, skip2 = 0; char c;
443 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
445 /* remove trailing ep or e.p. (nonstandard PGN) */
446 if (yytext[yyleng-1] == 'p') {
448 yytext[yyleng] = NULLCHAR;
449 } else if (yytext[yyleng-1] == '.') {
451 yytext[yyleng] = NULLCHAR;
454 /* remove the [xX:-] and =() */
455 if ((yytext[1] == 'x') || (yytext[1] == 'X')
456 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
457 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
458 if (yytext[2+skip1+skip2] == '(') skip2++;
460 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
462 cl.ffIn = yytext[0] - AAA;
464 cl.ftIn = yytext[1+skip1] - AAA;
465 c = cl.promoCharIn = yytext[2+skip1+skip2];
467 /* [HGM] do not allow values beyond board size */
468 if(cl.ffIn >= BOARD_RGHT ||
469 cl.ffIn < BOARD_LEFT ||
470 cl.ftIn >= BOARD_RGHT ||
471 cl.ftIn < BOARD_LEFT )
472 return ImpossibleMove;
474 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
477 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
479 currentMoveString[0] = cl.ff + AAA;
480 currentMoveString[1] = cl.rf + ONE;
481 currentMoveString[2] = cl.ft + AAA;
482 currentMoveString[3] = cl.rt + ONE;
483 currentMoveString[4] = cl.promoChar;
484 currentMoveString[5] = NULLCHAR;
486 return (int) cl.kind;
489 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
491 * unambiguously abbreviated Pawn capture, possibly with promotion
494 ChessMove result; char c;
496 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
498 /* remove trailing ep or e.p. (nonstandard PGN) */
499 if (yytext[yyleng-1] == 'p') {
501 yytext[yyleng] = NULLCHAR;
502 } else if (yytext[yyleng-1] == '.') {
504 yytext[yyleng] = NULLCHAR;
507 /* remove the [xX:-] */
508 if ((yytext[1] == 'x') || (yytext[1] == 'X')
509 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
511 currentMoveString[0] = yytext[0];
512 currentMoveString[2] = yytext[1+skip];
513 currentMoveString[3] = yytext[2+skip];
515 /* [HGM] do not allow values beyond board size */
516 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
517 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
518 currentMoveString[3] - ONE < 0 ||
519 currentMoveString[2] - AAA >= BOARD_RGHT ||
520 currentMoveString[0] - AAA < BOARD_LEFT ||
521 currentMoveString[2] - AAA < BOARD_LEFT )
522 return ImpossibleMove;
524 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
525 currentMoveString[0] != currentMoveString[2] ) {
526 currentMoveString[1] = yytext[2+skip];
528 if (WhiteOnMove(yyboardindex)) {
529 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
530 currentMoveString[1] = yytext[2+skip] - 1;
531 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn)
532 return ImpossibleMove;
534 currentMoveString[1] = currentMoveString[3] + 1;
535 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
536 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn)
537 return ImpossibleMove;
539 if (yyleng-skip > 3) {
540 if (yytext[yyleng-1] == ')')
541 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
543 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
544 currentMoveString[5] = NULLCHAR;
545 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
548 currentMoveString[4] = NULLCHAR;
551 result = LegalityTest(boards[yyboardindex],
552 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
553 currentMoveString[1] - ONE,
554 currentMoveString[0] - AAA,
555 currentMoveString[3] - ONE,
556 currentMoveString[2] - AAA,
557 currentMoveString[4]);
559 if (currentMoveString[4] == NULLCHAR &&
560 (result == WhitePromotion || result == BlackPromotion)) {
561 currentMoveString[4] = PieceToChar(BlackQueen);
562 // [HGM] shatranj: take care of variants without Queen
563 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
564 currentMoveString[4] = PieceToChar(BlackFerz);
565 if(gameInfo.variant == VariantGreat)
566 currentMoveString[4] = PieceToChar(BlackMan);
567 currentMoveString[5] = NULLCHAR;
570 if (result != IllegalMove) return (int) result;
572 /* Special case: improperly written en passant capture */
573 if (WhiteOnMove(yyboardindex)) {
574 if (currentMoveString[3] == '5') {
575 currentMoveString[1] = '5';
576 currentMoveString[3] = '6';
578 return (int) IllegalMove;
581 if (currentMoveString[3] == '4') {
582 currentMoveString[1] = '4';
583 currentMoveString[3] = '3';
585 return (int) IllegalMove;
589 result = LegalityTest(boards[yyboardindex],
590 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
591 currentMoveString[1] - ONE,
592 currentMoveString[0] - AAA,
593 currentMoveString[3] - ONE,
594 currentMoveString[2] - AAA,
595 currentMoveString[4]);
597 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
600 return (int) IllegalMove;
603 "+"?[A-Z][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
605 * piece move, possibly ambiguous
607 DisambiguateClosure cl;
608 int skip = 0, skip2 = 0, promoted = 0;
610 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
612 if(yytext[0] == '+') promoted = skip = skip2 = 1;
614 /* remove the [xX:-] */
615 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
616 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
618 if (WhiteOnMove(yyboardindex)) {
619 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
621 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
623 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
627 cl.rtIn = yytext[2+skip] - ONE;
628 cl.ftIn = yytext[1+skip] - AAA;
629 cl.promoCharIn = NULLCHAR;
631 if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
632 cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
634 if (appData.debugMode) {
635 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
637 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
640 /* [HGM] but do not allow values beyond board size */
641 if(cl.rtIn >= BOARD_HEIGHT ||
643 cl.ftIn >= BOARD_RGHT ||
644 cl.ftIn < BOARD_LEFT )
645 return ImpossibleMove;
647 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
649 currentMoveString[0] = cl.ff + AAA;
650 currentMoveString[1] = cl.rf + ONE;
651 currentMoveString[2] = cl.ft + AAA;
652 currentMoveString[3] = cl.rt + ONE;
653 currentMoveString[4] = cl.promoChar;
654 currentMoveString[5] = NULLCHAR;
656 return (int) cl.kind;
659 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
661 * piece move with rank or file disambiguator
663 DisambiguateClosure cl;
664 int skip = 0, skip2 = 0; int promoted=0;
666 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
668 if(yytext[0]=='+') promoted = skip = skip2 = 1;
670 /* remove the [xX:-] */
671 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
672 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
674 if (WhiteOnMove(yyboardindex)) {
675 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
677 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
679 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
681 if (isalpha(yytext[1+skip2])) {
683 cl.ffIn = yytext[1+skip2] - AAA;
685 if(cl.ffIn >= BOARD_RGHT ||
686 cl.ffIn < BOARD_LEFT ) return 0;
688 cl.rfIn = yytext[1+skip2] - ONE;
690 if(cl.rfIn >= BOARD_HEIGHT ||
691 cl.rfIn < 0) return 0;
693 cl.rtIn = yytext[3+skip] - ONE;
694 cl.ftIn = yytext[2+skip] - AAA;
695 cl.promoCharIn = NULLCHAR;
697 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
698 cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
700 /* [HGM] do not allow values beyond board size */
701 if(cl.rtIn >= BOARD_HEIGHT ||
703 cl.ftIn >= BOARD_RGHT ||
704 cl.ftIn < BOARD_LEFT )
705 return ImpossibleMove;
707 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
709 currentMoveString[0] = cl.ff + AAA;
710 currentMoveString[1] = cl.rf + ONE;
711 currentMoveString[2] = cl.ft + AAA;
712 currentMoveString[3] = cl.rt + ONE;
713 currentMoveString[4] = cl.promoChar;
714 currentMoveString[5] = NULLCHAR;
716 return (int) cl.kind;
719 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
722 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
724 /* [HGM] all squares referenced to board edges in stead of absolute */
725 if (WhiteOnMove(yyboardindex)) {
726 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
727 /* ICS wild castling */
729 ff = (BOARD_WIDTH-1)>>1;
739 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
740 /* ICS wild castling */
742 ff = (BOARD_WIDTH-1)>>1;
752 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
754 if (WhiteOnMove(yyboardindex)) {
755 ff = initialRights[2];
756 ft = initialRights[1];
758 ff = initialRights[5];
759 ft = initialRights[4];
761 if (appData.debugMode)
763 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
765 if(ff < 0 || ft < 0) return 0;
767 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
768 if (appData.debugMode) {
769 fprintf(debugFP, "long castling %d %d\n", ff, ft);
771 return (int) LegalityTest(boards[yyboardindex],
772 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
773 rf, ff, rt, ft, NULLCHAR);
776 00|0-0|oo|OO|o-o|O-O {
779 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
781 if (WhiteOnMove(yyboardindex)) {
782 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
783 /* ICS wild castling */
785 ff = (BOARD_WIDTH-1)>>1;
795 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
796 /* ICS wild castling */
798 ff = (BOARD_WIDTH-1)>>1;
808 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
809 if (WhiteOnMove(yyboardindex)) {
810 ff = initialRights[2];
811 ft = initialRights[0];
813 ff = initialRights[5];
814 ft = initialRights[3];
816 if (appData.debugMode) {
817 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
819 if(ff < 0 || ft < 0) return 0;
821 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
822 if (appData.debugMode) {
823 fprintf(debugFP, "short castling %d %d\n", ff, ft);
826 return (int) LegalityTest(boards[yyboardindex],
827 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
828 rf, ff, rt, ft, NULLCHAR);
831 [A-Za-z][@*][a-l][0-9] {
832 /* Bughouse piece drop. */
833 currentMoveString[1] = '@';
834 currentMoveString[2] = yytext[2];
835 currentMoveString[3] = yytext[3];
836 currentMoveString[4] = NULLCHAR;
838 if (appData.debugMode) {
839 fprintf(debugFP, "Drop: %s\n", currentMoveString);
841 /* [HGM] do not allow values beyond board size */
842 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
843 currentMoveString[2] - AAA >= BOARD_WIDTH )
844 return ImpossibleMove;
846 if (WhiteOnMove(yyboardindex)) {
847 currentMoveString[0] = ToUpper(yytext[0]);
849 currentMoveString[0] = ToLower(yytext[0]);
851 return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), DROP_RANK, // [HGM] does drops now too
852 CharToPiece(currentMoveString[0]), currentMoveString[3] - ONE, currentMoveString[2] - AAA, NULLCHAR);
856 if (WhiteOnMove(yyboardindex))
857 return (int) BlackWins;
859 return (int) WhiteWins;
862 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
863 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
866 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
867 return (int) GameUnfinished;
871 return (int) GameIsDrawn;
875 return (int) GameIsDrawn;
879 if (WhiteOnMove(yyboardindex))
880 return (int) BlackWins;
882 return (int) WhiteWins;
886 if (WhiteOnMove(yyboardindex))
887 return (int) BlackWins;
889 return (int) WhiteWins;
892 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
893 return (int) GameIsDrawn;
896 [Dd]raw(n)?(" (".*")")? {
897 return (int) GameIsDrawn;
900 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) {
901 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
904 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) {
905 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
908 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
909 return (int) WhiteWins;
912 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
913 return (int) BlackWins;
916 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
917 return (int) GameIsDrawn;
920 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
921 return (int) GameUnfinished;
924 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
926 if ((yyleng == 1) && (yytext[0] == '1'))
927 return (int) MoveNumberOne;
930 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
931 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
932 return (int) ElapsedTime;
936 /* position diagram enclosed in [-- --] */
937 return (int) PositionDiagram;
940 ^"{--------------"\n[^\}]*\n"--------------}"$ {
941 /* position diagram enclosed in {-- --} */
942 return (int) PositionDiagram;
945 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
949 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
950 return (int) GNUChessGame;
953 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
954 return (int) XBoardGame;
957 \$[0-9]+ { /* numeric annotation glyph */
961 \{[^\}]*\} { /* anything in {} */
962 return (int) Comment;
965 ;.*$ { /* ; to end of line */
966 return (int) Comment;
969 \[[^\]]*\] { /* anything in [] */
970 return (int) Comment;
973 \([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */
974 return (int) Comment;
977 \([^)][^)]+\) { /* >=2 chars in () */
978 return (int) Comment;
981 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
982 /* Skip mail headers */
986 /* Skip random words */
990 /* Skip everything else */
996 static char *StringToLex;
1005 if (StringToLex != NULL) {
1007 if (ret == NULLCHAR)
1011 } else if (unputCount > 0) {
1012 ret = unputBuffer[--unputCount];
1024 * Return offset of next pattern within current file
1028 int offset = ftell(lexFP) - unputCount;
1036 static void output(ch)
1039 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",
1043 static void unput(ch)
1046 if (ch == 0) return;
1047 if (StringToLex != NULL) {
1050 if (unputCount >= UNPUT_BUF_SIZE)
1051 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
1053 unputBuffer[unputCount++] = ch;
1057 /* Get ready to lex from a new file. Kludge below sticks
1058 an artificial newline at the front of the file, which the
1059 above grammar ignores, but which makes ^ at start of pattern
1060 match at the real start of the file.
1068 unput('\n'); /* kludge */
1071 /* Get ready to lex from a string. ^ at start of pattern WON'T
1072 match at the start of the string!
1081 #endif /*!FLEX_SCANNER*/
1084 void my_yy_input(buf, result, max_size)
1091 if (StringToLex != NULL) {
1093 while (*StringToLex != NULLCHAR) {
1094 *buf++ = *StringToLex++;
1100 count = fread(buf, 1, max_size, yyin);
1110 static YY_BUFFER_STATE my_file_buffer = NULL;
1113 Return offset of next pattern in the current file.
1117 int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
1119 return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
1127 if (my_file_buffer != NULL)
1128 yy_delete_buffer(my_file_buffer);
1130 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1131 yy_switch_to_buffer(my_file_buffer);
1137 if (my_file_buffer != NULL)
1138 yy_delete_buffer(my_file_buffer);
1140 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
1141 yy_switch_to_buffer(my_file_buffer);
1143 #endif /*FLEX_SCANNER*/
1150 /* Parse a move from the given string s */
1151 /* ^ at start of pattern WON'T work here unless using flex */
1152 ChessMove yylexstr(boardIndex, s, text, len)
1153 int boardIndex, len;
1157 char *oldStringToLex;
1159 YY_BUFFER_STATE buffer, oldBuffer;
1162 yyboardindex = boardIndex;
1163 oldStringToLex = StringToLex;
1166 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
1167 oldBuffer = YY_CURRENT_BUFFER;
1168 yy_switch_to_buffer(buffer);
1169 #endif /*FLEX_SCANNER*/
1171 ret = (ChessMove) yylex();
1172 strncpy(text, yy_text, len-1); // [HGM] vari: yy_text is not available to caller after buffer switch ?!?
1173 text[len-1] = NULLCHAR;
1176 if (oldBuffer != NULL)
1177 yy_switch_to_buffer(oldBuffer);
1178 yy_delete_buffer(buffer);
1179 #endif /*FLEX_SCANNER*/
1180 StringToLex = oldStringToLex;