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
235 return ImpossibleMove;
\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
303 return ImpossibleMove;
\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) {
314 if(result == WhitePromotionKnight || result == BlackPromotionKnight ||
315 result == WhitePromotionQueen || result == BlackPromotionQueen) {
316 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
317 currentMoveString[4] = PieceToChar(BlackFerz);
318 else if(gameInfo.variant == VariantGreat)
319 currentMoveString[4] = PieceToChar(BlackMan);
321 currentMoveString[4] = PieceToChar(BlackQueen);
322 currentMoveString[5] = NULLCHAR;
324 } else if(appData.testLegality && // strip off unnecessary and false promo characters
325 !(result == WhitePromotionQueen || result == BlackPromotionQueen ||
326 result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
331 [a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
\r
333 * Pawn move, possibly with promotion
\r
335 DisambiguateClosure cl;
\r
336 int skip = 0; char c;
\r
338 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
340 /* remove the =() */
\r
341 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
\r
342 if (yytext[2+skip] == '(') skip++;
\r
344 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
346 cl.ffIn = yytext[0] - AAA;
\r
347 cl.rtIn = yytext[1] - ONE;
\r
348 cl.ftIn = yytext[0] - AAA;
\r
349 c = cl.promoCharIn = ToLower(yytext[2+skip]);
\r
351 /* [HGM] do not allow values beyond board size */
\r
352 if(cl.rtIn >= BOARD_HEIGHT ||
\r
354 cl.ffIn >= BOARD_RGHT ||
\r
355 cl.ftIn < BOARD_LEFT )
\r
356 return ImpossibleMove;
\r
358 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
359 return IllegalMove;
\r
362 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
364 currentMoveString[0] = cl.ff + AAA;
\r
365 currentMoveString[1] = cl.rf + ONE;
\r
366 currentMoveString[2] = cl.ft + AAA;
\r
367 currentMoveString[3] = cl.rt + ONE;
\r
368 currentMoveString[4] = cl.promoChar;
\r
369 currentMoveString[5] = NULLCHAR;
\r
371 return (int) cl.kind;
\r
375 (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
377 * Pawn capture, possibly with promotion, possibly ambiguous
\r
379 DisambiguateClosure cl;
\r
380 int skip1 = 0, skip2 = 0; char c;
\r
382 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
384 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
385 if (yytext[yyleng-1] == 'p') {
\r
387 yytext[yyleng] = NULLCHAR;
\r
388 } else if (yytext[yyleng-1] == '.') {
\r
390 yytext[yyleng] = NULLCHAR;
\r
393 /* remove the [xX:-] and =() */
\r
394 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
395 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
\r
396 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
\r
397 if (yytext[2+skip1+skip2] == '(') skip2++;
\r
399 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
401 cl.ffIn = yytext[0] - AAA;
\r
403 cl.ftIn = yytext[1+skip1] - AAA;
\r
404 c = cl.promoCharIn = yytext[2+skip1+skip2];
\r
406 /* [HGM] do not allow values beyond board size */
\r
407 if(cl.ffIn >= BOARD_RGHT ||
\r
408 cl.ffIn < BOARD_LEFT ||
\r
409 cl.ftIn >= BOARD_RGHT ||
\r
410 cl.ftIn < BOARD_LEFT )
\r
411 return ImpossibleMove;
\r
413 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
414 return IllegalMove;
\r
416 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
418 currentMoveString[0] = cl.ff + AAA;
\r
419 currentMoveString[1] = cl.rf + ONE;
\r
420 currentMoveString[2] = cl.ft + AAA;
\r
421 currentMoveString[3] = cl.rt + ONE;
\r
422 currentMoveString[4] = cl.promoChar;
\r
423 currentMoveString[5] = NULLCHAR;
\r
425 return (int) cl.kind;
\r
428 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
\r
430 * unambiguously abbreviated Pawn capture, possibly with promotion
\r
433 ChessMove result; char c;
\r
435 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
437 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
438 if (yytext[yyleng-1] == 'p') {
\r
440 yytext[yyleng] = NULLCHAR;
\r
441 } else if (yytext[yyleng-1] == '.') {
\r
443 yytext[yyleng] = NULLCHAR;
\r
446 /* remove the [xX:-] */
\r
447 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
448 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
\r
450 currentMoveString[0] = yytext[0];
\r
451 currentMoveString[2] = yytext[1+skip];
\r
452 currentMoveString[3] = yytext[2+skip];
\r
454 /* [HGM] do not allow values beyond board size */
\r
455 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
456 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
457 currentMoveString[3] - ONE < 0 ||
\r
458 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
459 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
460 currentMoveString[2] - AAA < BOARD_LEFT )
\r
461 return ImpossibleMove;
\r
463 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
\r
464 currentMoveString[0] != currentMoveString[2] ) {
\r
465 currentMoveString[1] = yytext[2+skip];
\r
467 if (WhiteOnMove(yyboardindex)) {
\r
468 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
\r
469 currentMoveString[1] = yytext[2+skip] - 1;
\r
470 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn)
\r
471 return ImpossibleMove;
\r
473 currentMoveString[1] = currentMoveString[3] + 1;
\r
474 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
\r
475 if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn)
\r
476 return ImpossibleMove;
\r
478 if (yyleng-skip > 3) {
\r
479 if (yytext[yyleng-1] == ')')
\r
480 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
482 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
483 currentMoveString[5] = NULLCHAR;
\r
484 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
485 return IllegalMove;
\r
487 currentMoveString[4] = NULLCHAR;
\r
490 result = LegalityTest(boards[yyboardindex],
\r
491 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
492 currentMoveString[1] - ONE,
\r
493 currentMoveString[0] - AAA,
\r
494 currentMoveString[3] - ONE,
\r
495 currentMoveString[2] - AAA,
\r
496 currentMoveString[4]);
\r
498 if (currentMoveString[4] == NULLCHAR &&
\r
499 (result == WhitePromotionQueen || result == BlackPromotionQueen ||
\r
500 result == WhitePromotionKnight || result == BlackPromotionKnight)) {
\r
501 currentMoveString[4] = PieceToChar(BlackQueen);
\r
502 // [HGM] shatranj: take care of variants without Queen
\r
503 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
\r
504 currentMoveString[4] = PieceToChar(BlackFerz);
\r
505 if(gameInfo.variant == VariantGreat)
\r
506 currentMoveString[4] = PieceToChar(BlackMan);
\r
507 currentMoveString[5] = NULLCHAR;
\r
510 if (result != IllegalMove) return (int) result;
\r
512 /* Special case: improperly written en passant capture */
\r
513 if (WhiteOnMove(yyboardindex)) {
\r
514 if (currentMoveString[3] == '5') {
\r
515 currentMoveString[1] = '5';
\r
516 currentMoveString[3] = '6';
\r
518 return (int) IllegalMove;
\r
521 if (currentMoveString[3] == '4') {
\r
522 currentMoveString[1] = '4';
\r
523 currentMoveString[3] = '3';
\r
525 return (int) IllegalMove;
\r
529 result = LegalityTest(boards[yyboardindex],
\r
530 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
\r
531 currentMoveString[1] - ONE,
\r
532 currentMoveString[0] - AAA,
\r
533 currentMoveString[3] - ONE,
\r
534 currentMoveString[2] - AAA,
\r
535 currentMoveString[4]);
\r
537 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
\r
538 return (int) result;
\r
540 return (int) IllegalMove;
\r
543 "+"?[A-Z][xX:-]?[a-l][0-9]=? {
\r
545 * piece move, possibly ambiguous
\r
547 DisambiguateClosure cl;
\r
548 int skip = 0, skip2 = 0, promoted = 0;
\r
550 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
552 if(yytext[0] == '+') promoted = skip = skip2 = 1;
\r
554 /* remove the [xX:-] */
\r
555 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
\r
556 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
\r
558 if (WhiteOnMove(yyboardindex)) {
\r
559 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
561 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
563 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
567 cl.rtIn = yytext[2+skip] - ONE;
\r
568 cl.ftIn = yytext[1+skip] - AAA;
\r
569 cl.promoCharIn = NULLCHAR;
\r
571 if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
\r
572 cl.promoCharIn = yytext[yyleng-1];
\r
574 if (appData.debugMode) {
\r
575 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
\r
577 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
\r
580 /* [HGM] but do not allow values beyond board size */
\r
581 if(cl.rtIn >= BOARD_HEIGHT ||
\r
583 cl.ftIn >= BOARD_RGHT ||
\r
584 cl.ftIn < BOARD_LEFT )
\r
585 return ImpossibleMove;
\r
587 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
589 currentMoveString[0] = cl.ff + AAA;
\r
590 currentMoveString[1] = cl.rf + ONE;
\r
591 currentMoveString[2] = cl.ft + AAA;
\r
592 currentMoveString[3] = cl.rt + ONE;
\r
593 currentMoveString[4] = cl.promoChar;
\r
594 currentMoveString[5] = NULLCHAR;
\r
596 return (int) cl.kind;
\r
599 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=? {
\r
601 * piece move with rank or file disambiguator
\r
603 DisambiguateClosure cl;
\r
604 int skip = 0, skip2 = 0; int promoted=0;
\r
606 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
608 if(yytext[0]=='+') promoted = skip = skip2 = 1;
\r
610 /* remove the [xX:-] */
\r
611 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
\r
612 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
\r
614 if (WhiteOnMove(yyboardindex)) {
\r
615 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
617 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
619 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
621 if (isalpha(yytext[1+skip2])) {
\r
623 cl.ffIn = yytext[1+skip2] - AAA;
\r
625 if(cl.ffIn >= BOARD_RGHT ||
\r
626 cl.ffIn < BOARD_LEFT ) return 0;
\r
628 cl.rfIn = yytext[1+skip2] - ONE;
\r
630 if(cl.rfIn >= BOARD_HEIGHT ||
\r
631 cl.rfIn < 0) return 0;
\r
633 cl.rtIn = yytext[3+skip] - ONE;
\r
634 cl.ftIn = yytext[2+skip] - AAA;
\r
635 cl.promoCharIn = NULLCHAR;
\r
637 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
\r
638 cl.promoCharIn = yytext[yyleng-1];
\r
640 /* [HGM] do not allow values beyond board size */
\r
641 if(cl.rtIn >= BOARD_HEIGHT ||
\r
643 cl.ftIn >= BOARD_RGHT ||
\r
644 cl.ftIn < BOARD_LEFT )
\r
645 return ImpossibleMove;
\r
647 Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
\r
649 currentMoveString[0] = cl.ff + AAA;
\r
650 currentMoveString[1] = cl.rf + ONE;
\r
651 currentMoveString[2] = cl.ft + AAA;
\r
652 currentMoveString[3] = cl.rt + ONE;
\r
653 currentMoveString[4] = cl.promoChar;
\r
654 currentMoveString[5] = NULLCHAR;
\r
656 return (int) cl.kind;
\r
659 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
\r
660 int rf, ff, rt, ft;
\r
662 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
664 /* [HGM] all squares referenced to board edges in stead of absolute */
\r
665 if (WhiteOnMove(yyboardindex)) {
\r
666 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
667 /* ICS wild castling */
\r
669 ff = (BOARD_WIDTH-1)>>1;
\r
674 ff = BOARD_WIDTH>>1;
\r
679 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
680 /* ICS wild castling */
\r
681 rf = BOARD_HEIGHT-1;
\r
682 ff = (BOARD_WIDTH-1)>>1;
\r
683 rt = BOARD_HEIGHT-1;
\r
686 rf = BOARD_HEIGHT-1;
\r
687 ff = BOARD_WIDTH>>1;
\r
688 rt = BOARD_HEIGHT-1;
\r
692 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
693 if (WhiteOnMove(yyboardindex)) {
\r
694 ff = initialRights[2];
\r
695 ft = initialRights[1];
\r
697 ff = initialRights[5];
\r
698 ft = initialRights[4];
\r
700 if (appData.debugMode)
\r
702 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
\r
704 if(ff < 0 || ft < 0) return 0;
\r
706 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
707 if (appData.debugMode) {
\r
708 fprintf(debugFP, "long castling %d %d\n", ff, ft);
\r
710 return (int) LegalityTest(boards[yyboardindex],
\r
711 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
\r
712 rf, ff, rt, ft, NULLCHAR);
\r
715 00|0-0|oo|OO|o-o|O-O {
\r
716 int rf, ff, rt, ft;
\r
718 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
720 if (WhiteOnMove(yyboardindex)) {
\r
721 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
722 /* ICS wild castling */
\r
724 ff = (BOARD_WIDTH-1)>>1;
\r
729 ff = BOARD_WIDTH>>1;
\r
734 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
735 /* ICS wild castling */
\r
736 rf = BOARD_HEIGHT-1;
\r
737 ff = (BOARD_WIDTH-1)>>1;
\r
738 rt = BOARD_HEIGHT-1;
\r
741 rf = BOARD_HEIGHT-1;
\r
742 ff = BOARD_WIDTH>>1;
\r
743 rt = BOARD_HEIGHT-1;
\r
747 if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
748 if (WhiteOnMove(yyboardindex)) {
\r
749 ff = initialRights[2];
\r
750 ft = initialRights[0];
\r
752 ff = initialRights[5];
\r
753 ft = initialRights[3];
\r
755 if (appData.debugMode) {
\r
756 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
\r
758 if(ff < 0 || ft < 0) return 0;
\r
760 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
761 if (appData.debugMode) {
\r
762 fprintf(debugFP, "short castling %d %d\n", ff, ft);
\r
765 return (int) LegalityTest(boards[yyboardindex],
\r
766 PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
\r
767 rf, ff, rt, ft, NULLCHAR);
\r
770 [A-Z][@*][a-l][0-9] {
\r
771 /* Bughouse piece drop. No legality checking for now. */
\r
772 currentMoveString[1] = '@';
\r
773 currentMoveString[2] = yytext[2];
\r
774 currentMoveString[3] = yytext[3];
\r
775 currentMoveString[4] = NULLCHAR;
\r
777 if (appData.debugMode) {
\r
778 fprintf(debugFP, "Drop: %s\n", currentMoveString);
\r
780 /* [HGM] do not allow values beyond board size */
\r
781 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
782 currentMoveString[2] - AAA >= BOARD_WIDTH )
\r
783 return ImpossibleMove;
\r
785 if (WhiteOnMove(yyboardindex)) {
\r
786 currentMoveString[0] = ToUpper(yytext[0]);
\r
787 return (int) WhiteDrop;
\r
789 currentMoveString[0] = ToLower(yytext[0]);
\r
790 return (int) BlackDrop;
\r
795 if (WhiteOnMove(yyboardindex))
\r
796 return (int) BlackWins;
\r
798 return (int) WhiteWins;
\r
801 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
\r
802 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
805 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
\r
806 return (int) GameUnfinished;
\r
810 return (int) GameIsDrawn;
\r
814 return (int) GameIsDrawn;
\r
817 ([Cc]heck)?[Mm]ate {
\r
818 if (WhiteOnMove(yyboardindex))
\r
819 return (int) BlackWins;
\r
821 return (int) WhiteWins;
\r
825 if (WhiteOnMove(yyboardindex))
\r
826 return (int) BlackWins;
\r
828 return (int) WhiteWins;
\r
831 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
\r
832 return (int) GameIsDrawn;
\r
835 [Dd]raw(n)?(" (".*")")? {
\r
836 return (int) GameIsDrawn;
\r
839 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) {
\r
840 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
\r
843 (([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) {
\r
844 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
847 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
\r
848 return (int) WhiteWins;
\r
851 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
\r
852 return (int) BlackWins;
\r
855 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
\r
856 return (int) GameIsDrawn;
\r
859 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
\r
860 return (int) GameUnfinished;
\r
863 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
\r
865 if ((yyleng == 1) && (yytext[0] == '1'))
\r
866 return (int) MoveNumberOne;
\r
869 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
\r
870 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
\r
871 return (int) ElapsedTime;
\r
875 /* position diagram enclosed in [-- --] */
\r
876 return (int) PositionDiagram;
\r
879 ^"{--------------"\n[^\}]*\n"--------------}"$ {
\r
880 /* position diagram enclosed in {-- --} */
\r
881 return (int) PositionDiagram;
\r
884 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
\r
885 return (int) PGNTag;
\r
888 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
\r
889 return (int) GNUChessGame;
\r
892 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
\r
893 return (int) XBoardGame;
\r
896 \$[0-9]+ { /* numeric annotation glyph */
\r
900 \{[^\}]*\} { /* anything in {} */
\r
901 return (int) Comment;
\r
904 ;.*$ { /* ; to end of line */
\r
905 return (int) Comment;
\r
908 \[[^\]]*\] { /* anything in [] */
\r
909 return (int) Comment;
\r
912 \([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */
\r
913 return (int) Comment;
\r
916 \([^)][^)]+\) { /* >=2 chars in () */
\r
917 return (int) Comment;
\r
920 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
\r
921 /* Skip mail headers */
\r
925 /* Skip random words */
\r
929 /* Skip everything else */
\r
935 static char *StringToLex;
\r
937 #ifndef FLEX_SCANNER
\r
938 static FILE *lexFP;
\r
944 if (StringToLex != NULL) {
\r
945 ret = *StringToLex;
\r
946 if (ret == NULLCHAR)
\r
950 } else if (unputCount > 0) {
\r
951 ret = unputBuffer[--unputCount];
\r
953 ret = fgetc(lexFP);
\r
963 * Return offset of next pattern within current file
\r
967 int offset = ftell(lexFP) - unputCount;
\r
975 static void output(ch)
\r
978 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",
\r
982 static void unput(ch)
\r
985 if (ch == 0) return;
\r
986 if (StringToLex != NULL) {
\r
989 if (unputCount >= UNPUT_BUF_SIZE)
\r
990 if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
\r
992 unputBuffer[unputCount++] = ch;
\r
996 /* Get ready to lex from a new file. Kludge below sticks
\r
997 an artificial newline at the front of the file, which the
\r
998 above grammar ignores, but which makes ^ at start of pattern
\r
999 match at the real start of the file.
\r
1005 StringToLex = NULL;
\r
1007 unput('\n'); /* kludge */
\r
1010 /* Get ready to lex from a string. ^ at start of pattern WON'T
\r
1011 match at the start of the string!
\r
1020 #endif /*!FLEX_SCANNER*/
\r
1022 #ifdef FLEX_SCANNER
\r
1023 void my_yy_input(buf, result, max_size)
\r
1030 if (StringToLex != NULL) {
\r
1032 while (*StringToLex != NULLCHAR) {
\r
1033 *buf++ = *StringToLex++;
\r
1039 count = fread(buf, 1, max_size, yyin);
\r
1041 *result = YY_NULL;
\r
1049 static YY_BUFFER_STATE my_file_buffer = NULL;
\r
1052 Return offset of next pattern in the current file.
\r
1056 int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
\r
1058 return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
\r
1059 yy_n_chars + pos);
\r
1066 if (my_file_buffer != NULL)
\r
1067 yy_delete_buffer(my_file_buffer);
\r
1069 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1070 yy_switch_to_buffer(my_file_buffer);
\r
1076 if (my_file_buffer != NULL)
\r
1077 yy_delete_buffer(my_file_buffer);
\r
1078 StringToLex = NULL;
\r
1079 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
\r
1080 yy_switch_to_buffer(my_file_buffer);
\r
1082 #endif /*FLEX_SCANNER*/
\r
1089 /* Parse a move from the given string s */
\r
1090 /* ^ at start of pattern WON'T work here unless using flex */
\r
1091 ChessMove yylexstr(boardIndex, s, text, len)
\r
1092 int boardIndex, len;
\r
1096 char *oldStringToLex;
\r
1097 #ifdef FLEX_SCANNER
\r
1098 YY_BUFFER_STATE buffer, oldBuffer;
\r
1101 yyboardindex = boardIndex;
\r
1102 oldStringToLex = StringToLex;
\r
1104 #ifdef FLEX_SCANNER
\r
1105 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1106 oldBuffer = YY_CURRENT_BUFFER;
\r
1107 yy_switch_to_buffer(buffer);
\r
1108 #endif /*FLEX_SCANNER*/
\r
1110 ret = (ChessMove) yylex();
1111 strncpy(text, yy_text, len-1); // [HGM] vari: yy_text is not available to caller after buffer switch ?!?
1112 text[len-1] = NULLCHAR;
\r
1114 #ifdef FLEX_SCANNER
\r
1115 if (oldBuffer != NULL)
\r
1116 yy_switch_to_buffer(oldBuffer);
\r
1117 yy_delete_buffer(buffer);
\r
1118 #endif /*FLEX_SCANNER*/
\r
1119 StringToLex = oldStringToLex;
\r