9 * parser.l -- lex parser of algebraic chess moves for XBoard
\r
11 * Copyright 1991 by Digital Equipment Corporation, Maynard,
\r
14 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005,
\r
15 * 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
\r
17 * The following terms apply to Digital Equipment Corporation's copyright
\r
18 * interest in XBoard:
\r
19 * ------------------------------------------------------------------------
\r
20 * All Rights Reserved
\r
22 * Permission to use, copy, modify, and distribute this software and its
\r
23 * documentation for any purpose and without fee is hereby granted,
\r
24 * provided that the above copyright notice appear in all copies and that
\r
25 * both that copyright notice and this permission notice appear in
\r
26 * supporting documentation, and that the name of Digital not be
\r
27 * used in advertising or publicity pertaining to distribution of the
\r
28 * software without specific, written prior permission.
\r
30 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
\r
31 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
\r
32 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
\r
33 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
\r
34 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
\r
35 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
\r
37 * ------------------------------------------------------------------------
\r
39 * The following terms apply to the enhanced version of XBoard
\r
40 * distributed by the Free Software Foundation:
\r
41 * ------------------------------------------------------------------------
\r
43 * GNU XBoard is free software: you can redistribute it and/or modify
\r
44 * it under the terms of the GNU General Public License as published by
\r
45 * the Free Software Foundation, either version 3 of the License, or (at
\r
46 * your option) any later version.
\r
48 * GNU XBoard is distributed in the hope that it will be useful, but
\r
49 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
51 * General Public License for more details.
\r
53 * You should have received a copy of the GNU General Public License
\r
54 * along with this program. If not, see http://www.gnu.org/licenses/.
\r
56 *------------------------------------------------------------------------
\r
57 ** See the file ChangeLog for a revision history. */
\r
59 /* This parser handles all forms of promotion.
\r
60 * The parser resolves ambiguous moves by searching and check-testing.
\r
61 * It also parses comments of the form [anything] or (anything).
\r
63 * [HGM] Parser extensively modified for bigger boards, Shogi-like syntax,
\r
64 * and unknow pieces. All pieces are now mandatory upper case, but can be
\r
65 * any letter A-Z. Files must be lower case (as before), but can run upto 'l'.
\r
66 * Ranks can be 0-9. The parser returns 0 for off-board files and ranks.
\r
67 * For an unknown piece (as mover or promotion piece) it returns
\r
68 * IllegalMove, like it does when the piece doesn't match.
\r
69 * Promotions can now also be appended Shogi-style, a bare '=' or '+',
\r
70 * and this is then returned as promotion character. The piece indicator
\r
71 * can be prefixed by a '+' to indicate it is a promoted piece.
\r
76 #define NO_CONSTRAINT -1
\r
79 #define UNPUT_BUF_SIZE YYLMAX
\r
82 /* yytext is probably a char*, but could be a char[]. yy_text is set
\r
83 in YY_DECL below, because if yytext is a char*, its value is not
\r
86 #else /*!FLEX_SCANNER*/
\r
87 /* yytext is definitely a char[], so yy_text can be set here, statically. */
\r
88 char *yy_text = (char *) yytext;
\r
93 /* [AP] use prototypes in function declarations */
\r
94 #define YY_USE_PROTOS
\r
96 #ifdef YY_USE_PROTOS
\r
97 #define YY_PROTO(proto) proto
\r
99 #define YY_PROTO(proto) ()
\r
101 /* end of [AP] fix */
\r
104 #define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size)
\r
107 int _yylex YY_PROTO((void)); \
\r
108 int yylex YY_PROTO((void)) \
\r
110 int result = _yylex(); \
\r
111 yy_text = (char *) yytext; \
\r
114 int _yylex YY_PROTO((void))
\r
122 /* The includes must be here, below the #undef input */
\r
127 # include <stdlib.h>
\r
128 # include <string.h>
\r
129 #else /* not STDC_HEADERS */
\r
131 # include <string.h>
\r
132 # else /* not HAVE_STRING_H */
\r
133 # include <strings.h>
\r
134 # endif /* not HAVE_STRING_H */
\r
135 #endif /* not STDC_HEADERS */
\r
138 # include <unistd.h>
\r
141 #if defined(_amigados)
\r
142 # include <errno.h>
\r
144 # include <fcntl.h> /* isatty() prototype */
\r
145 # endif /* HAVE_FCNTL_H */
\r
146 #endif /* defined(_amigados) */
\r
148 #include "common.h"
\r
149 #include "backend.h"
\r
150 #include "frontend.h"
\r
151 #include "parser.h"
\r
154 extern int PosFlags P((int));
\r
156 extern Board boards[MAX_MOVES];
\r
158 int yyskipmoves = FALSE;
\r
159 char currentMoveString[YYLMAX];
\r
160 #ifndef FLEX_SCANNER
\r
161 char unputBuffer[UNPUT_BUF_SIZE];
\r
162 int unputCount = 0;
\r
165 #ifdef FLEX_SCANNER
\r
166 void my_yy_input P((char *buf, int *result, int max_size));
\r
167 #else /*!FLEX_SCANNER*/
\r
168 static int input P((void));
\r
169 static void output P((int ch));
\r
170 static void unput P((int ch));
\r
171 int yylook P((void));
\r
172 int yyback P((int *, int));
\r
175 int yywrap P((void));
\r
176 extern void CopyBoard P((Board to, Board from));
\r
181 "+"?[A-Z][/]?[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
\r
183 * Fully-qualified algebraic move, possibly with promotion
\r
185 int skip1 = 0, skip2 = 0, skip3 = 0, promoted = 0;
\r
190 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
192 if (yytext[0] == '+') skip1 = skip3 = promoted = 1; /* [HGM] Shogi promoted */
\r
195 if (yytext[1+skip1] == '/') skip1++;
\r
197 /* remove the [xX:-] */
\r
198 if ((yytext[3+skip1] == 'x') || (yytext[3+skip1] == 'X') ||
\r
199 (yytext[3+skip1] == '-') || (yytext[3+skip1] == ':')) skip2 = 1;
\r
201 currentMoveString[0] = yytext[1+skip1];
\r
202 currentMoveString[1] = yytext[2+skip1];
\r
203 currentMoveString[2] = yytext[3+skip1+skip2];
\r
204 currentMoveString[3] = yytext[4+skip1+skip2];
\r
205 currentMoveString[4] = NULLCHAR;
\r
207 if (appData.debugMode) {
\r
208 fprintf(debugFP, "Parser Qa1b2: yyleng=%d\n",
\r
212 if (yyleng-skip1-skip2 > 5) { char c;
\r
213 if (yytext[yyleng-1] == ')') {
\r
214 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
216 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
218 currentMoveString[5] = NULLCHAR;
\r
219 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
220 return IllegalMove; /* [HGM] promotion to invalid piece */
\r
223 if (appData.debugMode) {
\r
224 fprintf(debugFP, "parser: %s\n", currentMoveString);
\r
226 /* [HGM] do not allow values beyond board size */
\r
227 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
\r
228 currentMoveString[1] - ONE < 0 ||
\r
229 currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
230 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
231 currentMoveString[3] - ONE < 0 ||
\r
232 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
233 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
234 currentMoveString[2] - AAA < BOARD_LEFT )
\r
237 piece = boards[yyboardindex]
\r
238 [currentMoveString[1] - ONE][currentMoveString[0] - AAA];
\r
239 if(promoted) piece = (ChessSquare) (DEMOTED piece);
\r
240 c = PieceToChar(piece);
\r
241 if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));
\r
242 if (ToLower(yytext[skip3]) != ToLower(c))
\r
243 return (int) IllegalMove;
\r
245 result = LegalityTest(boards[yyboardindex],
\r
246 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
247 currentMoveString[1] - ONE,
\r
248 currentMoveString[0] - AAA,
\r
249 currentMoveString[3] - ONE,
\r
250 currentMoveString[2] - AAA,
\r
251 currentMoveString[4]);
\r
253 if (currentMoveString[4] == NULLCHAR &&
\r
254 (result == WhitePromotionKnight || result == BlackPromotionKnight ||
\r
255 result == WhitePromotionQueen || result == BlackPromotionQueen)) {
\r
256 currentMoveString[4] = PieceToChar(BlackQueen);
\r
257 currentMoveString[5] = NULLCHAR;
\r
260 return (int) result;
\r
263 [a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
\r
265 * Simple algebraic move, possibly with promotion
\r
266 * [HGM] Engine moves are received in this format, with lower-case promoChar!
\r
271 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
273 /* remove the [xX:-] */
\r
274 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
\r
275 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
\r
277 currentMoveString[0] = yytext[0];
\r
278 currentMoveString[1] = yytext[1];
\r
279 currentMoveString[2] = yytext[2+skip];
\r
280 currentMoveString[3] = yytext[3+skip];
\r
281 currentMoveString[4] = NULLCHAR;
\r
283 if (yyleng-skip > 4) { char c;
\r
284 if (yytext[yyleng-1] == ')') {
\r
285 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
287 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
289 currentMoveString[5] = NULLCHAR;
\r
290 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
291 return IllegalMove;
\r
294 /* [HGM] do not allow values beyond board size */
\r
295 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
\r
296 currentMoveString[1] - ONE < 0 ||
\r
297 currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
298 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
299 currentMoveString[3] - ONE < 0 ||
\r
300 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
301 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
302 currentMoveString[2] - AAA < BOARD_LEFT )
\r
305 result = LegalityTest(boards[yyboardindex],
\r
306 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
307 currentMoveString[1] - ONE,
\r
308 currentMoveString[0] - AAA,
\r
309 currentMoveString[3] - ONE,
\r
310 currentMoveString[2] - AAA,
\r
311 currentMoveString[4]);
\r
313 if (currentMoveString[4] == NULLCHAR &&
\r
314 (result == WhitePromotionKnight || result == BlackPromotionKnight ||
\r
315 result == WhitePromotionQueen || result == BlackPromotionQueen)) {
\r
316 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
\r
317 currentMoveString[4] = PieceToChar(BlackFerz);
\r
318 else if(gameInfo.variant == VariantGreat)
\r
319 currentMoveString[4] = PieceToChar(BlackMan);
\r
321 currentMoveString[4] = PieceToChar(BlackQueen);
\r
322 currentMoveString[5] = NULLCHAR;
\r
325 return (int) result;
\r
328 [a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
\r
330 * Pawn move, possibly with promotion
\r
332 DisambiguateClosure cl;
\r
333 int skip = 0; char c;
\r
335 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
337 /* remove the =() */
\r
338 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
\r
339 if (yytext[2+skip] == '(') skip++;
\r
341 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
343 cl.ffIn = yytext[0] - AAA;
\r
344 cl.rtIn = yytext[1] - ONE;
\r
345 cl.ftIn = yytext[0] - AAA;
\r
346 c = cl.promoCharIn = yytext[2+skip];
\r
348 /* [HGM] do not allow values beyond board size */
\r
349 if(cl.rtIn >= BOARD_HEIGHT ||
\r
351 cl.ffIn >= BOARD_RGHT ||
\r
352 cl.ftIn < BOARD_LEFT )
\r
355 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
356 return IllegalMove;
\r
359 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
361 currentMoveString[0] = cl.ff + AAA;
\r
362 currentMoveString[1] = cl.rf + ONE;
\r
363 currentMoveString[2] = cl.ft + AAA;
\r
364 currentMoveString[3] = cl.rt + ONE;
\r
365 currentMoveString[4] = cl.promoChar;
\r
366 currentMoveString[5] = NULLCHAR;
\r
368 return (int) cl.kind;
\r
372 (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."|=)? {
\r
374 * Pawn capture, possibly with promotion, possibly ambiguous
\r
376 DisambiguateClosure cl;
\r
377 int skip1 = 0, skip2 = 0; char c;
\r
379 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
381 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
382 if (yytext[yyleng-1] == 'p') {
\r
384 yytext[yyleng] = NULLCHAR;
\r
385 } else if (yytext[yyleng-1] == '.') {
\r
387 yytext[yyleng] = NULLCHAR;
\r
390 /* remove the [xX:-] and =() */
\r
391 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
392 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
\r
393 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
\r
394 if (yytext[2+skip1+skip2] == '(') skip2++;
\r
396 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
398 cl.ffIn = yytext[0] - AAA;
\r
400 cl.ftIn = yytext[1+skip1] - AAA;
\r
401 c = cl.promoCharIn = yytext[2+skip1+skip2];
\r
403 /* [HGM] do not allow values beyond board size */
\r
404 if(cl.ffIn >= BOARD_RGHT ||
\r
405 cl.ffIn < BOARD_LEFT ||
\r
406 cl.ftIn >= BOARD_RGHT ||
\r
407 cl.ftIn < BOARD_LEFT )
\r
410 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
411 return IllegalMove;
\r
413 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
415 currentMoveString[0] = cl.ff + AAA;
\r
416 currentMoveString[1] = cl.rf + ONE;
\r
417 currentMoveString[2] = cl.ft + AAA;
\r
418 currentMoveString[3] = cl.rt + ONE;
\r
419 currentMoveString[4] = cl.promoChar;
\r
420 currentMoveString[5] = NULLCHAR;
\r
422 return (int) cl.kind;
\r
425 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
\r
427 * unambiguously abbreviated Pawn capture, possibly with promotion
\r
430 ChessMove result; char c;
\r
432 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
434 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
435 if (yytext[yyleng-1] == 'p') {
\r
437 yytext[yyleng] = NULLCHAR;
\r
438 } else if (yytext[yyleng-1] == '.') {
\r
440 yytext[yyleng] = NULLCHAR;
\r
443 /* remove the [xX:-] */
\r
444 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
445 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
\r
447 currentMoveString[0] = yytext[0];
\r
448 currentMoveString[2] = yytext[1+skip];
\r
449 currentMoveString[3] = yytext[2+skip];
\r
451 /* [HGM] do not allow values beyond board size */
\r
452 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
453 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
454 currentMoveString[3] - ONE < 0 ||
\r
455 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
456 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
457 currentMoveString[2] - AAA < BOARD_LEFT )
\r
460 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
\r
461 currentMoveString[0] != currentMoveString[2] ) {
\r
462 currentMoveString[1] = yytext[2+skip];
\r
464 if (WhiteOnMove(yyboardindex)) {
\r
465 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
\r
466 currentMoveString[1] = yytext[2+skip] - 1;
\r
467 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn)
\r
468 return ImpossibleMove;
\r
470 currentMoveString[1] = currentMoveString[3] + 1;
\r
471 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
\r
472 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn)
\r
473 return ImpossibleMove;
\r
475 if (yyleng-skip > 3) {
\r
476 if (yytext[yyleng-1] == ')')
\r
477 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
479 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
480 currentMoveString[5] = NULLCHAR;
\r
481 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
482 return IllegalMove;
\r
484 currentMoveString[4] = NULLCHAR;
\r
487 result = LegalityTest(boards[yyboardindex],
\r
488 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
489 currentMoveString[1] - ONE,
\r
490 currentMoveString[0] - AAA,
\r
491 currentMoveString[3] - ONE,
\r
492 currentMoveString[2] - AAA,
\r
493 currentMoveString[4]);
\r
495 if (currentMoveString[4] == NULLCHAR &&
\r
496 (result == WhitePromotionQueen || result == BlackPromotionQueen ||
\r
497 result == WhitePromotionKnight || result == BlackPromotionKnight)) {
\r
498 currentMoveString[4] = PieceToChar(BlackQueen);
\r
499 // [HGM] shatranj: take care of variants without Queen
\r
500 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
\r
501 currentMoveString[4] = PieceToChar(BlackFerz);
\r
502 if(gameInfo.variant == VariantGreat)
\r
503 currentMoveString[4] = PieceToChar(BlackMan);
\r
504 currentMoveString[5] = NULLCHAR;
\r
507 if (result != IllegalMove) return (int) result;
\r
509 /* Special case: improperly written en passant capture */
\r
510 if (WhiteOnMove(yyboardindex)) {
\r
511 if (currentMoveString[3] == '5') {
\r
512 currentMoveString[1] = '5';
\r
513 currentMoveString[3] = '6';
\r
515 return (int) IllegalMove;
\r
518 if (currentMoveString[3] == '4') {
\r
519 currentMoveString[1] = '4';
\r
520 currentMoveString[3] = '3';
\r
522 return (int) IllegalMove;
\r
526 result = LegalityTest(boards[yyboardindex],
\r
527 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
528 currentMoveString[1] - ONE,
\r
529 currentMoveString[0] - AAA,
\r
530 currentMoveString[3] - ONE,
\r
531 currentMoveString[2] - AAA,
\r
532 currentMoveString[4]);
\r
534 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
\r
535 return (int) result;
\r
537 return (int) IllegalMove;
\r
540 "+"?[A-Z][xX:-]?[a-l][0-9]=? {
\r
542 * piece move, possibly ambiguous
\r
544 DisambiguateClosure cl;
\r
545 int skip = 0, skip2 = 0, promoted = 0;
\r
547 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
549 if(yytext[0] == '+') promoted = skip = skip2 = 1;
\r
551 /* remove the [xX:-] */
\r
552 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
\r
553 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
\r
555 if (WhiteOnMove(yyboardindex)) {
\r
556 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
558 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
560 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
564 cl.rtIn = yytext[2+skip] - ONE;
\r
565 cl.ftIn = yytext[1+skip] - AAA;
\r
566 cl.promoCharIn = NULLCHAR;
\r
568 if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
\r
569 cl.promoCharIn = yytext[yyleng-1];
\r
571 if (appData.debugMode) {
\r
572 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
\r
574 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
\r
577 /* [HGM] but do not allow values beyond board size */
\r
578 if(cl.rtIn >= BOARD_HEIGHT ||
\r
580 cl.ftIn >= BOARD_RGHT ||
\r
581 cl.ftIn < BOARD_LEFT )
\r
584 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
586 currentMoveString[0] = cl.ff + AAA;
\r
587 currentMoveString[1] = cl.rf + ONE;
\r
588 currentMoveString[2] = cl.ft + AAA;
\r
589 currentMoveString[3] = cl.rt + ONE;
\r
590 currentMoveString[4] = cl.promoChar;
\r
591 currentMoveString[5] = NULLCHAR;
\r
593 return (int) cl.kind;
\r
596 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=? {
\r
598 * piece move with rank or file disambiguator
\r
600 DisambiguateClosure cl;
\r
601 int skip = 0, skip2 = 0; int promoted=0;
\r
603 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
605 if(yytext[0]=='+') promoted = skip = skip2 = 1;
\r
607 /* remove the [xX:-] */
\r
608 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
\r
609 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
\r
611 if (WhiteOnMove(yyboardindex)) {
\r
612 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
614 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
616 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
618 if (isalpha(yytext[1+skip2])) {
\r
620 cl.ffIn = yytext[1+skip2] - AAA;
\r
622 if(cl.ffIn >= BOARD_RGHT ||
\r
623 cl.ffIn < BOARD_LEFT ) return 0;
\r
625 cl.rfIn = yytext[1+skip2] - ONE;
\r
627 if(cl.rfIn >= BOARD_HEIGHT ||
\r
628 cl.rfIn < 0) return 0;
\r
630 cl.rtIn = yytext[3+skip] - ONE;
\r
631 cl.ftIn = yytext[2+skip] - AAA;
\r
632 cl.promoCharIn = NULLCHAR;
\r
634 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
\r
635 cl.promoCharIn = yytext[yyleng-1];
\r
637 /* [HGM] do not allow values beyond board size */
\r
638 if(cl.rtIn >= BOARD_HEIGHT ||
\r
640 cl.ftIn >= BOARD_RGHT ||
\r
641 cl.ftIn < BOARD_LEFT )
\r
644 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
646 currentMoveString[0] = cl.ff + AAA;
\r
647 currentMoveString[1] = cl.rf + ONE;
\r
648 currentMoveString[2] = cl.ft + AAA;
\r
649 currentMoveString[3] = cl.rt + ONE;
\r
650 currentMoveString[4] = cl.promoChar;
\r
651 currentMoveString[5] = NULLCHAR;
\r
653 return (int) cl.kind;
\r
656 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
\r
657 int rf, ff, rt, ft;
\r
659 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
661 /* [HGM] all squares referenced to board edges in stead of absolute */
\r
662 if (WhiteOnMove(yyboardindex)) {
\r
663 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
664 /* ICS wild castling */
\r
666 ff = (BOARD_WIDTH-1)>>1;
\r
671 ff = BOARD_WIDTH>>1;
\r
676 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
677 /* ICS wild castling */
\r
678 rf = BOARD_HEIGHT-1;
\r
679 ff = (BOARD_WIDTH-1)>>1;
\r
680 rt = BOARD_HEIGHT-1;
\r
683 rf = BOARD_HEIGHT-1;
\r
684 ff = BOARD_WIDTH>>1;
\r
685 rt = BOARD_HEIGHT-1;
\r
689 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
690 if (WhiteOnMove(yyboardindex)) {
\r
691 ff = initialRights[2];
\r
692 ft = initialRights[1];
\r
694 ff = initialRights[5];
\r
695 ft = initialRights[4];
\r
697 if (appData.debugMode)
\r
699 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
\r
701 if(ff < 0 || ft < 0) return 0;
\r
703 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
704 if (appData.debugMode) {
\r
705 fprintf(debugFP, "long castling %d %d\n", ff, ft);
\r
707 return (int) LegalityTest(boards[yyboardindex],
\r
708 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
\r
709 rf, ff, rt, ft, NULLCHAR);
\r
712 00|0-0|oo|OO|o-o|O-O {
\r
713 int rf, ff, rt, ft;
\r
715 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
717 if (WhiteOnMove(yyboardindex)) {
\r
718 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
719 /* ICS wild castling */
\r
721 ff = (BOARD_WIDTH-1)>>1;
\r
726 ff = BOARD_WIDTH>>1;
\r
731 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
732 /* ICS wild castling */
\r
733 rf = BOARD_HEIGHT-1;
\r
734 ff = (BOARD_WIDTH-1)>>1;
\r
735 rt = BOARD_HEIGHT-1;
\r
738 rf = BOARD_HEIGHT-1;
\r
739 ff = BOARD_WIDTH>>1;
\r
740 rt = BOARD_HEIGHT-1;
\r
744 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
745 if (WhiteOnMove(yyboardindex)) {
\r
746 ff = initialRights[2];
\r
747 ft = initialRights[0];
\r
749 ff = initialRights[5];
\r
750 ft = initialRights[3];
\r
752 if (appData.debugMode) {
\r
753 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
\r
755 if(ff < 0 || ft < 0) return 0;
\r
757 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
758 if (appData.debugMode) {
\r
759 fprintf(debugFP, "short castling %d %d\n", ff, ft);
\r
762 return (int) LegalityTest(boards[yyboardindex],
\r
763 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
\r
764 rf, ff, rt, ft, NULLCHAR);
\r
767 [A-Z][@*][a-l][0-9] {
\r
768 /* Bughouse piece drop. No legality checking for now. */
\r
769 currentMoveString[1] = '@';
\r
770 currentMoveString[2] = yytext[2];
\r
771 currentMoveString[3] = yytext[3];
\r
772 currentMoveString[4] = NULLCHAR;
\r
774 if (appData.debugMode) {
\r
775 fprintf(debugFP, "Drop: %s\n", currentMoveString);
\r
777 /* [HGM] do not allow values beyond board size */
\r
778 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
779 currentMoveString[2] - AAA >= BOARD_WIDTH )
\r
782 if (WhiteOnMove(yyboardindex)) {
\r
783 currentMoveString[0] = ToUpper(yytext[0]);
\r
784 return (int) WhiteDrop;
\r
786 currentMoveString[0] = ToLower(yytext[0]);
\r
787 return (int) BlackDrop;
\r
792 if (WhiteOnMove(yyboardindex))
\r
793 return (int) BlackWins;
\r
795 return (int) WhiteWins;
\r
798 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
\r
799 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
802 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
\r
803 return (int) GameUnfinished;
\r
807 return (int) GameIsDrawn;
\r
811 return (int) GameIsDrawn;
\r
814 ([Cc]heck)?[Mm]ate {
\r
815 if (WhiteOnMove(yyboardindex))
\r
816 return (int) BlackWins;
\r
818 return (int) WhiteWins;
\r
822 if (WhiteOnMove(yyboardindex))
\r
823 return (int) BlackWins;
\r
825 return (int) WhiteWins;
\r
828 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
\r
829 return (int) GameIsDrawn;
\r
832 [Dd]raw(n)?(" (".*")")? {
\r
833 return (int) GameIsDrawn;
\r
836 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) {
\r
837 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
\r
840 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) {
\r
841 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
844 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
\r
845 return (int) WhiteWins;
\r
848 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
\r
849 return (int) BlackWins;
\r
852 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
\r
853 return (int) GameIsDrawn;
\r
856 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
\r
857 return (int) GameUnfinished;
\r
860 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
\r
862 if ((yyleng == 1) && (yytext[0] == '1'))
\r
863 return (int) MoveNumberOne;
\r
866 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
\r
867 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
\r
868 return (int) ElapsedTime;
\r
872 /* position diagram enclosed in [-- --] */
\r
873 return (int) PositionDiagram;
\r
876 ^"{--------------"\n[^\}]*\n"--------------}"$ {
\r
877 /* position diagram enclosed in {-- --} */
\r
878 return (int) PositionDiagram;
\r
881 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
\r
882 return (int) PGNTag;
\r
885 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
\r
886 return (int) GNUChessGame;
\r
889 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
\r
890 return (int) XBoardGame;
\r
893 \$[0-9]+ { /* numeric annotation glyph */
\r
897 \{[^\}]*\} { /* anything in {} */
\r
898 return (int) Comment;
\r
901 ;.*$ { /* ; to end of line */
\r
902 return (int) Comment;
\r
905 \[[^\]]*\] { /* anything in [] */
\r
906 return (int) Comment;
\r
909 \([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */
\r
910 return (int) Comment;
\r
913 \([^)][^)]+\) { /* >=2 chars in () */
\r
914 return (int) Comment;
\r
917 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
\r
918 /* Skip mail headers */
\r
922 /* Skip random words */
\r
926 /* Skip everything else */
\r
932 static char *StringToLex;
\r
934 #ifndef FLEX_SCANNER
\r
935 static FILE *lexFP;
\r
941 if (StringToLex != NULL) {
\r
942 ret = *StringToLex;
\r
943 if (ret == NULLCHAR)
\r
947 } else if (unputCount > 0) {
\r
948 ret = unputBuffer[--unputCount];
\r
950 ret = fgetc(lexFP);
\r
960 * Return offset of next pattern within current file
\r
964 int offset = ftell(lexFP) - unputCount;
\r
972 static void output(ch)
\r
975 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",
\r
979 static void unput(ch)
\r
982 if (ch == 0) return;
\r
983 if (StringToLex != NULL) {
\r
986 if (unputCount >= UNPUT_BUF_SIZE)
\r
987 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
\r
989 unputBuffer[unputCount++] = ch;
\r
993 /* Get ready to lex from a new file. Kludge below sticks
\r
994 an artificial newline at the front of the file, which the
\r
995 above grammar ignores, but which makes ^ at start of pattern
\r
996 match at the real start of the file.
\r
1002 StringToLex = NULL;
\r
1004 unput('\n'); /* kludge */
\r
1007 /* Get ready to lex from a string. ^ at start of pattern WON'T
\r
1008 match at the start of the string!
\r
1017 #endif /*!FLEX_SCANNER*/
\r
1019 #ifdef FLEX_SCANNER
\r
1020 void my_yy_input(buf, result, max_size)
\r
1027 if (StringToLex != NULL) {
\r
1029 while (*StringToLex != NULLCHAR) {
\r
1030 *buf++ = *StringToLex++;
\r
1036 count = fread(buf, 1, max_size, yyin);
\r
1038 *result = YY_NULL;
\r
1046 static YY_BUFFER_STATE my_file_buffer = NULL;
\r
1049 Return offset of next pattern in the current file.
\r
1053 int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
\r
1055 return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
\r
1056 yy_n_chars + pos);
\r
1063 if (my_file_buffer != NULL)
\r
1064 yy_delete_buffer(my_file_buffer);
\r
1066 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1067 yy_switch_to_buffer(my_file_buffer);
\r
1073 if (my_file_buffer != NULL)
\r
1074 yy_delete_buffer(my_file_buffer);
\r
1075 StringToLex = NULL;
\r
1076 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
\r
1077 yy_switch_to_buffer(my_file_buffer);
\r
1079 #endif /*FLEX_SCANNER*/
\r
1086 /* Parse a move from the given string s */
\r
1087 /* ^ at start of pattern WON'T work here unless using flex */
\r
1088 ChessMove yylexstr(boardIndex, s)
\r
1093 char *oldStringToLex;
\r
1094 #ifdef FLEX_SCANNER
\r
1095 YY_BUFFER_STATE buffer, oldBuffer;
\r
1098 yyboardindex = boardIndex;
\r
1099 oldStringToLex = StringToLex;
\r
1101 #ifdef FLEX_SCANNER
\r
1102 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1103 oldBuffer = YY_CURRENT_BUFFER;
\r
1104 yy_switch_to_buffer(buffer);
\r
1105 #endif /*FLEX_SCANNER*/
\r
1107 ret = (ChessMove) yylex();
\r
1109 #ifdef FLEX_SCANNER
\r
1110 if (oldBuffer != NULL)
\r
1111 yy_switch_to_buffer(oldBuffer);
\r
1112 yy_delete_buffer(buffer);
\r
1113 #endif /*FLEX_SCANNER*/
\r
1114 StringToLex = oldStringToLex;
\r