9 * parser.l -- lex parser of algebraic chess moves for XBoard
\r
10 * $Id: parser.l,v 2.1 2003/10/27 19:21:00 mann Exp $
\r
12 * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
\r
13 * Enhancements Copyright 1992-95 Free Software Foundation, Inc.
\r
15 * The following terms apply to Digital Equipment Corporation's copyright
\r
16 * interest in XBoard:
\r
17 * ------------------------------------------------------------------------
\r
18 * All Rights Reserved
\r
20 * Permission to use, copy, modify, and distribute this software and its
\r
21 * documentation for any purpose and without fee is hereby granted,
\r
22 * provided that the above copyright notice appear in all copies and that
\r
23 * both that copyright notice and this permission notice appear in
\r
24 * supporting documentation, and that the name of Digital not be
\r
25 * used in advertising or publicity pertaining to distribution of the
\r
26 * software without specific, written prior permission.
\r
28 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
\r
29 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
\r
30 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
\r
31 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
\r
32 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
\r
33 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
\r
35 * ------------------------------------------------------------------------
\r
37 * The following terms apply to the enhanced version of XBoard distributed
\r
38 * by the Free Software Foundation:
\r
39 * ------------------------------------------------------------------------
\r
40 * This program is free software; you can redistribute it and/or modify
\r
41 * it under the terms of the GNU General Public License as published by
\r
42 * the Free Software Foundation; either version 2 of the License, or
\r
43 * (at your option) any later version.
\r
45 * This program is distributed in the hope that it will be useful,
\r
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
48 * GNU General Public License for more details.
\r
50 * You should have received a copy of the GNU General Public License
\r
51 * along with this program; if not, write to the Free Software
\r
52 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
53 * ------------------------------------------------------------------------
\r
56 /* This parser handles all forms of promotion.
\r
57 * The parser resolves ambiguous moves by searching and check-testing.
\r
58 * It also parses comments of the form [anything] or (anything).
\r
60 * [HGM] Parser extensively modified for bigger boards, Shogi-like syntax,
\r
61 * and unknow pieces. All pieces are now mandatory upper case, but can be
\r
62 * any letter A-Z. Files must be lower case (as before), but can run upto 'l'.
\r
63 * Ranks can be 0-9. The parser returns 0 for off-board files and ranks.
\r
64 * For an unknown piece (as mover or promotion piece) it returns
\r
65 * IllegalMove, like it does when the piece doesn't match.
\r
66 * Promotions can now also be appended Shogi-style, a bare '=' or '+',
\r
67 * and this is then returned as promotion character. The piece indicator
\r
68 * can be prefixed by a '+' to indicate it is a promoted piece.
\r
73 #define NO_CONSTRAINT -1
\r
76 #define UNPUT_BUF_SIZE YYLMAX
\r
79 /* yytext is probably a char*, but could be a char[]. yy_text is set
\r
80 in YY_DECL below, because if yytext is a char*, its value is not
\r
83 #else /*!FLEX_SCANNER*/
\r
84 /* yytext is definitely a char[], so yy_text can be set here, statically. */
\r
85 char *yy_text = (char *) yytext;
\r
91 #define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size)
\r
94 int _yylex YY_PROTO((void)); \
\r
95 int yylex YY_PROTO((void)) \
\r
97 int result = _yylex(); \
\r
98 yy_text = (char *) yytext; \
\r
101 int _yylex YY_PROTO((void))
\r
109 /* The includes must be here, below the #undef input */
\r
114 # include <stdlib.h>
\r
115 # include <string.h>
\r
116 #else /* not STDC_HEADERS */
\r
118 # include <string.h>
\r
119 # else /* not HAVE_STRING_H */
\r
120 # include <strings.h>
\r
121 # endif /* not HAVE_STRING_H */
\r
122 #endif /* not STDC_HEADERS */
\r
125 # include <unistd.h>
\r
128 #if defined(_amigados)
\r
129 # include <errno.h>
\r
131 # include <fcntl.h> /* isatty() prototype */
\r
132 # endif /* HAVE_FCNTL_H */
\r
133 #endif /* defined(_amigados) */
\r
135 #include "common.h"
\r
136 #include "backend.h"
\r
137 #include "frontend.h"
\r
138 #include "parser.h"
\r
141 extern int PosFlags P((int));
\r
143 extern Board boards[MAX_MOVES];
\r
145 int yyskipmoves = FALSE;
\r
146 char currentMoveString[YYLMAX];
\r
147 #ifndef FLEX_SCANNER
\r
148 char unputBuffer[UNPUT_BUF_SIZE];
\r
149 int unputCount = 0;
\r
152 #ifdef FLEX_SCANNER
\r
153 void my_yy_input P((char *buf, int *result, int max_size));
\r
154 #else /*!FLEX_SCANNER*/
\r
155 static int input P((void));
\r
156 static void output P((int ch));
\r
157 static void unput P((int ch));
\r
158 int yylook P((void));
\r
159 int yyback P((int *, int));
\r
162 int yywrap P((void));
\r
163 extern void CopyBoard P((Board to, Board from));
\r
168 "+"?[A-Z][/]?[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
\r
170 * Fully-qualified algebraic move, possibly with promotion
\r
172 int skip1 = 0, skip2 = 0, skip3 = 0, promoted = 0;
\r
177 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
179 if (yytext[0] == '+') skip1 = skip3 = promoted = 1; /* [HGM] Shogi promoted */
\r
182 if (yytext[1+skip1] == '/') skip1++;
\r
184 /* remove the [xX:-] */
\r
185 if ((yytext[3+skip1] == 'x') || (yytext[3+skip1] == 'X') ||
\r
186 (yytext[3+skip1] == '-') || (yytext[3+skip1] == ':')) skip2 = 1;
\r
188 currentMoveString[0] = yytext[1+skip1];
\r
189 currentMoveString[1] = yytext[2+skip1];
\r
190 currentMoveString[2] = yytext[3+skip1+skip2];
\r
191 currentMoveString[3] = yytext[4+skip1+skip2];
\r
192 currentMoveString[4] = NULLCHAR;
\r
194 if (appData.debugMode) {
\r
195 fprintf(debugFP, "Parser Qa1b2: yyleng=%d\n",
\r
199 if (yyleng-skip1-skip2 > 5) { char c;
\r
200 if (yytext[yyleng-1] == ')') {
\r
201 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
203 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
205 currentMoveString[5] = NULLCHAR;
\r
206 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
207 return IllegalMove; /* [HGM] promotion to invalid piece */
\r
210 if (appData.debugMode) {
\r
211 fprintf(debugFP, "parser: %s\n", currentMoveString);
\r
213 /* [HGM] do not allow values beyond board size */
\r
214 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
\r
215 currentMoveString[1] - ONE < 0 ||
\r
216 currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
217 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
218 currentMoveString[3] - ONE < 0 ||
\r
219 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
220 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
221 currentMoveString[2] - AAA < BOARD_LEFT )
\r
224 piece = boards[yyboardindex]
\r
225 [currentMoveString[1] - ONE][currentMoveString[0] - AAA];
\r
226 if(promoted) piece = (ChessSquare) (DEMOTED piece);
\r
227 c = PieceToChar(piece);
\r
228 if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));
\r
229 if (ToLower(yytext[skip3]) != ToLower(c))
\r
230 return (int) IllegalMove;
\r
232 result = LegalityTest(boards[yyboardindex],
\r
233 PosFlags(yyboardindex), EP_UNKNOWN,
\r
234 initialRights, /* [HGM] assume all castlings allowed */
\r
235 currentMoveString[1] - ONE,
\r
236 currentMoveString[0] - AAA,
\r
237 currentMoveString[3] - ONE,
\r
238 currentMoveString[2] - AAA,
\r
239 currentMoveString[4]);
\r
241 if (currentMoveString[4] == NULLCHAR &&
\r
242 (result == WhitePromotionKnight || result == BlackPromotionKnight ||
\r
243 result == WhitePromotionQueen || result == BlackPromotionQueen)) {
\r
244 currentMoveString[4] = PieceToChar(BlackQueen);
\r
245 currentMoveString[5] = NULLCHAR;
\r
248 return (int) result;
\r
251 [a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {
\r
253 * Simple algebraic move, possibly with promotion
\r
254 * [HGM] Engine moves are received in this format, with lower-case promoChar!
\r
259 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
261 /* remove the [xX:-] */
\r
262 if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
\r
263 (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
\r
265 currentMoveString[0] = yytext[0];
\r
266 currentMoveString[1] = yytext[1];
\r
267 currentMoveString[2] = yytext[2+skip];
\r
268 currentMoveString[3] = yytext[3+skip];
\r
269 currentMoveString[4] = NULLCHAR;
\r
271 if (yyleng-skip > 4) { char c;
\r
272 if (yytext[yyleng-1] == ')') {
\r
273 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
275 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
277 currentMoveString[5] = NULLCHAR;
\r
278 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
279 return IllegalMove;
\r
282 /* [HGM] do not allow values beyond board size */
\r
283 if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||
\r
284 currentMoveString[1] - ONE < 0 ||
\r
285 currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
286 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
287 currentMoveString[3] - ONE < 0 ||
\r
288 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
289 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
290 currentMoveString[2] - AAA < BOARD_LEFT )
\r
293 result = LegalityTest(boards[yyboardindex],
\r
294 PosFlags(yyboardindex), EP_UNKNOWN,
\r
295 initialRights, /* [HGM] assume all castlings allowed */
\r
296 currentMoveString[1] - ONE,
\r
297 currentMoveString[0] - AAA,
\r
298 currentMoveString[3] - ONE,
\r
299 currentMoveString[2] - AAA,
\r
300 currentMoveString[4]);
\r
302 if (currentMoveString[4] == NULLCHAR &&
\r
303 (result == WhitePromotionKnight || result == BlackPromotionKnight ||
\r
304 result == WhitePromotionQueen || result == BlackPromotionQueen)) {
\r
305 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier)
\r
306 currentMoveString[4] = PieceToChar(BlackFerz);
\r
307 else if(gameInfo.variant == VariantGreat)
\r
308 currentMoveString[4] = PieceToChar(BlackMan);
\r
310 currentMoveString[4] = PieceToChar(BlackQueen);
\r
311 currentMoveString[5] = NULLCHAR;
\r
314 return (int) result;
\r
317 [a-l][0-9]((=?\(?[A-Z]\)?)|=)? {
\r
319 * Pawn move, possibly with promotion
\r
321 DisambiguateClosure cl;
\r
322 int skip = 0; char c;
\r
324 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
326 /* remove the =() */
\r
327 if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;
\r
328 if (yytext[2+skip] == '(') skip++;
\r
330 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
332 cl.ffIn = yytext[0] - AAA;
\r
333 cl.rtIn = yytext[1] - ONE;
\r
334 cl.ftIn = yytext[0] - AAA;
\r
335 c = cl.promoCharIn = yytext[2+skip];
\r
337 /* [HGM] do not allow values beyond board size */
\r
338 if(cl.rtIn >= BOARD_HEIGHT ||
\r
340 cl.ffIn >= BOARD_RGHT ||
\r
341 cl.ftIn < BOARD_LEFT )
\r
344 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
345 return IllegalMove;
\r
348 Disambiguate(boards[yyboardindex],
\r
349 PosFlags(yyboardindex), EP_UNKNOWN, &cl);
\r
351 currentMoveString[0] = cl.ff + AAA;
\r
352 currentMoveString[1] = cl.rf + ONE;
\r
353 currentMoveString[2] = cl.ft + AAA;
\r
354 currentMoveString[3] = cl.rt + ONE;
\r
355 currentMoveString[4] = cl.promoChar;
\r
356 currentMoveString[5] = NULLCHAR;
\r
358 return (int) cl.kind;
\r
362 (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
364 * Pawn capture, possibly with promotion, possibly ambiguous
\r
366 DisambiguateClosure cl;
\r
367 int skip1 = 0, skip2 = 0; char c;
\r
369 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
371 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
372 if (yytext[yyleng-1] == 'p') {
\r
374 yytext[yyleng] = NULLCHAR;
\r
375 } else if (yytext[yyleng-1] == '.') {
\r
377 yytext[yyleng] = NULLCHAR;
\r
380 /* remove the [xX:-] and =() */
\r
381 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
382 || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
\r
383 if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;
\r
384 if (yytext[2+skip1+skip2] == '(') skip2++;
\r
386 cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
\r
388 cl.ffIn = yytext[0] - AAA;
\r
390 cl.ftIn = yytext[1+skip1] - AAA;
\r
391 c = cl.promoCharIn = yytext[2+skip1+skip2];
\r
393 /* [HGM] do not allow values beyond board size */
\r
394 if(cl.ffIn >= BOARD_RGHT ||
\r
395 cl.ffIn < BOARD_LEFT ||
\r
396 cl.ftIn >= BOARD_RGHT ||
\r
397 cl.ftIn < BOARD_LEFT )
\r
400 if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)
\r
401 return IllegalMove;
\r
403 Disambiguate(boards[yyboardindex],
\r
404 PosFlags(yyboardindex), EP_UNKNOWN, &cl);
\r
406 currentMoveString[0] = cl.ff + AAA;
\r
407 currentMoveString[1] = cl.rf + ONE;
\r
408 currentMoveString[2] = cl.ft + AAA;
\r
409 currentMoveString[3] = cl.rt + ONE;
\r
410 currentMoveString[4] = cl.promoChar;
\r
411 currentMoveString[5] = NULLCHAR;
\r
413 return (int) cl.kind;
\r
416 [a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {
\r
418 * unambiguously abbreviated Pawn capture, possibly with promotion
\r
421 ChessMove result; char c;
\r
423 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
425 /* remove trailing ep or e.p. (nonstandard PGN) */
\r
426 if (yytext[yyleng-1] == 'p') {
\r
428 yytext[yyleng] = NULLCHAR;
\r
429 } else if (yytext[yyleng-1] == '.') {
\r
431 yytext[yyleng] = NULLCHAR;
\r
434 /* remove the [xX:-] */
\r
435 if ((yytext[1] == 'x') || (yytext[1] == 'X')
\r
436 || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
\r
438 currentMoveString[0] = yytext[0];
\r
439 currentMoveString[2] = yytext[1+skip];
\r
440 currentMoveString[3] = yytext[2+skip];
\r
442 /* [HGM] do not allow values beyond board size */
\r
443 if(currentMoveString[0] - AAA >= BOARD_RGHT ||
\r
444 currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
445 currentMoveString[3] - ONE < 0 ||
\r
446 currentMoveString[2] - AAA >= BOARD_RGHT ||
\r
447 currentMoveString[0] - AAA < BOARD_LEFT ||
\r
448 currentMoveString[2] - AAA < BOARD_LEFT )
\r
451 if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */
\r
452 currentMoveString[0] != currentMoveString[2] ) {
\r
453 currentMoveString[1] = yytext[2+skip];
\r
455 if (WhiteOnMove(yyboardindex)) {
\r
456 if (yytext[2+skip] == ONE) return (int) ImpossibleMove;
\r
457 currentMoveString[1] = yytext[2+skip] - 1;
\r
459 currentMoveString[1] = currentMoveString[3] + 1;
\r
460 if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;
\r
462 if (yyleng-skip > 3) {
\r
463 if (yytext[yyleng-1] == ')')
\r
464 c = currentMoveString[4] = ToLower(yytext[yyleng-2]);
\r
466 c = currentMoveString[4] = ToLower(yytext[yyleng-1]);
\r
467 currentMoveString[5] = NULLCHAR;
\r
468 if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)
\r
469 return IllegalMove;
\r
471 currentMoveString[4] = NULLCHAR;
\r
474 result = LegalityTest(boards[yyboardindex],
\r
475 PosFlags(yyboardindex), EP_UNKNOWN,
\r
476 initialRights, /* [HGM] assume all castlings allowed */
\r
477 currentMoveString[1] - ONE,
\r
478 currentMoveString[0] - AAA,
\r
479 currentMoveString[3] - ONE,
\r
480 currentMoveString[2] - AAA,
\r
481 currentMoveString[4]);
\r
483 if (currentMoveString[4] == NULLCHAR &&
\r
484 (result == WhitePromotionQueen || result == BlackPromotionQueen ||
\r
485 result == WhitePromotionKnight || result == BlackPromotionKnight)) {
\r
486 currentMoveString[4] = PieceToChar(BlackQueen);
\r
487 // [HGM] shatranj: take care of variants without Queen
\r
488 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier)
\r
489 currentMoveString[4] = PieceToChar(BlackFerz);
\r
490 if(gameInfo.variant == VariantGreat)
\r
491 currentMoveString[4] = PieceToChar(BlackMan);
\r
492 currentMoveString[5] = NULLCHAR;
\r
495 if (result != IllegalMove) return (int) result;
\r
497 /* Special case: improperly written en passant capture */
\r
498 if (WhiteOnMove(yyboardindex)) {
\r
499 if (currentMoveString[3] == '5') {
\r
500 currentMoveString[1] = '5';
\r
501 currentMoveString[3] = '6';
\r
503 return (int) IllegalMove;
\r
506 if (currentMoveString[3] == '4') {
\r
507 currentMoveString[1] = '4';
\r
508 currentMoveString[3] = '3';
\r
510 return (int) IllegalMove;
\r
514 result = LegalityTest(boards[yyboardindex],
\r
515 PosFlags(yyboardindex), EP_UNKNOWN,
\r
516 initialRights, /* [HGM] assume all castlings allowed */
\r
517 currentMoveString[1] - ONE,
\r
518 currentMoveString[0] - AAA,
\r
519 currentMoveString[3] - ONE,
\r
520 currentMoveString[2] - AAA,
\r
521 currentMoveString[4]);
\r
523 if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
\r
524 return (int) result;
\r
526 return (int) IllegalMove;
\r
529 "+"?[A-Z][xX:-]?[a-l][0-9]=? {
\r
531 * piece move, possibly ambiguous
\r
533 DisambiguateClosure cl;
\r
534 int skip = 0, skip2 = 0, promoted = 0;
\r
536 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
538 if(yytext[0] == '+') promoted = skip = skip2 = 1;
\r
540 /* remove the [xX:-] */
\r
541 if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')
\r
542 || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;
\r
544 if (WhiteOnMove(yyboardindex)) {
\r
545 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
547 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
549 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
553 cl.rtIn = yytext[2+skip] - ONE;
\r
554 cl.ftIn = yytext[1+skip] - AAA;
\r
555 cl.promoCharIn = NULLCHAR;
\r
557 if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
\r
558 cl.promoCharIn = yytext[yyleng-1];
\r
560 if (appData.debugMode) {
\r
561 fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",
\r
563 cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');
\r
566 /* [HGM] but do not allow values beyond board size */
\r
567 if(cl.rtIn >= BOARD_HEIGHT ||
\r
569 cl.ftIn >= BOARD_RGHT ||
\r
570 cl.ftIn < BOARD_LEFT )
\r
573 Disambiguate(boards[yyboardindex],
\r
574 PosFlags(yyboardindex), EP_UNKNOWN, &cl);
\r
576 currentMoveString[0] = cl.ff + AAA;
\r
577 currentMoveString[1] = cl.rf + ONE;
\r
578 currentMoveString[2] = cl.ft + AAA;
\r
579 currentMoveString[3] = cl.rt + ONE;
\r
580 currentMoveString[4] = cl.promoChar;
\r
581 currentMoveString[5] = NULLCHAR;
\r
583 return (int) cl.kind;
\r
586 "+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=? {
\r
588 * piece move with rank or file disambiguator
\r
590 DisambiguateClosure cl;
\r
591 int skip = 0, skip2 = 0; int promoted=0;
\r
593 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
595 if(yytext[0]=='+') promoted = skip = skip2 = 1;
\r
597 /* remove the [xX:-] */
\r
598 if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')
\r
599 || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;
\r
601 if (WhiteOnMove(yyboardindex)) {
\r
602 cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));
\r
604 cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));
\r
606 if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);
\r
608 if (isalpha(yytext[1+skip2])) {
\r
610 cl.ffIn = yytext[1+skip2] - AAA;
\r
612 if(cl.ffIn >= BOARD_RGHT ||
\r
613 cl.ffIn < BOARD_LEFT ) return 0;
\r
615 cl.rfIn = yytext[1+skip2] - ONE;
\r
617 if(cl.rfIn >= BOARD_HEIGHT ||
\r
618 cl.rfIn < 0) return 0;
\r
620 cl.rtIn = yytext[3+skip] - ONE;
\r
621 cl.ftIn = yytext[2+skip] - AAA;
\r
622 cl.promoCharIn = NULLCHAR;
\r
624 if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
\r
625 cl.promoCharIn = yytext[yyleng-1];
\r
627 /* [HGM] do not allow values beyond board size */
\r
628 if(cl.rtIn >= BOARD_HEIGHT ||
\r
630 cl.ftIn >= BOARD_RGHT ||
\r
631 cl.ftIn < BOARD_LEFT )
\r
634 Disambiguate(boards[yyboardindex],
\r
635 PosFlags(yyboardindex), EP_UNKNOWN, &cl);
\r
637 currentMoveString[0] = cl.ff + AAA;
\r
638 currentMoveString[1] = cl.rf + ONE;
\r
639 currentMoveString[2] = cl.ft + AAA;
\r
640 currentMoveString[3] = cl.rt + ONE;
\r
641 currentMoveString[4] = cl.promoChar;
\r
642 currentMoveString[5] = NULLCHAR;
\r
644 return (int) cl.kind;
\r
647 000|0-0-0|ooo|OOO|o-o-o|O-O-O {
\r
648 int rf, ff, rt, ft;
\r
650 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
652 /* [HGM] all squares referenced to board edges in stead of absolute */
\r
653 if (WhiteOnMove(yyboardindex)) {
\r
654 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
655 /* ICS wild castling */
\r
657 ff = (BOARD_WIDTH-1)>>1;
\r
662 ff = BOARD_WIDTH>>1;
\r
667 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
668 /* ICS wild castling */
\r
669 rf = BOARD_HEIGHT-1;
\r
670 ff = (BOARD_WIDTH-1)>>1;
\r
671 rt = BOARD_HEIGHT-1;
\r
674 rf = BOARD_HEIGHT-1;
\r
675 ff = BOARD_WIDTH>>1;
\r
676 rt = BOARD_HEIGHT-1;
\r
680 if(gameInfo.variant == VariantFischeRandom) {
\r
681 if (WhiteOnMove(yyboardindex)) {
\r
682 ff = initialRights[2];
\r
683 ft = initialRights[1];
\r
685 ff = initialRights[5];
\r
686 ft = initialRights[4];
\r
688 fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
\r
689 if(ff < 0 || ft < 0) return 0;
\r
691 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
692 if (appData.debugMode) {
\r
693 fprintf(debugFP, "long castling %d %d\n", ff, ft);
\r
695 return (int) LegalityTest(boards[yyboardindex],
\r
696 PosFlags(yyboardindex), EP_UNKNOWN,
\r
697 castlingRights[yyboardindex], /* [HGM] use true castling rights */
\r
698 rf, ff, rt, ft, NULLCHAR);
\r
701 00|0-0|oo|OO|o-o|O-O {
\r
702 int rf, ff, rt, ft;
\r
704 if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
\r
706 if (WhiteOnMove(yyboardindex)) {
\r
707 if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {
\r
708 /* ICS wild castling */
\r
710 ff = (BOARD_WIDTH-1)>>1;
\r
715 ff = BOARD_WIDTH>>1;
\r
720 if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {
\r
721 /* ICS wild castling */
\r
722 rf = BOARD_HEIGHT-1;
\r
723 ff = (BOARD_WIDTH-1)>>1;
\r
724 rt = BOARD_HEIGHT-1;
\r
727 rf = BOARD_HEIGHT-1;
\r
728 ff = BOARD_WIDTH>>1;
\r
729 rt = BOARD_HEIGHT-1;
\r
733 if(gameInfo.variant == VariantFischeRandom) {
\r
734 if (WhiteOnMove(yyboardindex)) {
\r
735 ff = initialRights[2];
\r
736 ft = initialRights[0];
\r
738 ff = initialRights[5];
\r
739 ft = initialRights[3];
\r
741 if (appData.debugMode) {
\r
742 fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
\r
744 if(ff < 0 || ft < 0) return 0;
\r
746 sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
\r
747 if (appData.debugMode) {
\r
748 fprintf(debugFP, "short castling %d %d\n", ff, ft);
\r
751 return (int) LegalityTest(boards[yyboardindex],
\r
752 PosFlags(yyboardindex), EP_UNKNOWN,
\r
753 castlingRights[yyboardindex], /* [HGM] use true castling rights */
\r
754 rf, ff, rt, ft, NULLCHAR);
\r
757 [A-Z][@*][a-l][0-9] {
\r
758 /* Bughouse piece drop. No legality checking for now. */
\r
759 currentMoveString[1] = '@';
\r
760 currentMoveString[2] = yytext[2];
\r
761 currentMoveString[3] = yytext[3];
\r
762 currentMoveString[4] = NULLCHAR;
\r
764 if (appData.debugMode) {
\r
765 fprintf(debugFP, "Drop: %s\n", currentMoveString);
\r
767 /* [HGM] do not allow values beyond board size */
\r
768 if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||
\r
769 currentMoveString[2] - AAA >= BOARD_WIDTH )
\r
772 if (WhiteOnMove(yyboardindex)) {
\r
773 currentMoveString[0] = ToUpper(yytext[0]);
\r
774 return (int) WhiteDrop;
\r
776 currentMoveString[0] = ToLower(yytext[0]);
\r
777 return (int) BlackDrop;
\r
782 if (WhiteOnMove(yyboardindex))
\r
783 return (int) BlackWins;
\r
785 return (int) WhiteWins;
\r
788 (([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
\r
789 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
792 (([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
\r
793 return (int) GameUnfinished;
\r
797 return (int) GameIsDrawn;
\r
801 return (int) GameIsDrawn;
\r
804 ([Cc]heck)?[Mm]ate {
\r
805 if (WhiteOnMove(yyboardindex))
\r
806 return (int) BlackWins;
\r
808 return (int) WhiteWins;
\r
812 if (WhiteOnMove(yyboardindex))
\r
813 return (int) BlackWins;
\r
815 return (int) WhiteWins;
\r
818 [Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
\r
819 return (int) GameIsDrawn;
\r
822 [Dd]raw(n)?(" (".*")")? {
\r
823 return (int) GameIsDrawn;
\r
826 (([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ww][io]n(s)?.*) {
\r
827 return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
\r
830 (([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ll]os[tes]+.*) {
\r
831 return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
\r
834 ("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
\r
835 return (int) WhiteWins;
\r
838 ("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
\r
839 return (int) BlackWins;
\r
842 ("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
\r
843 return (int) GameIsDrawn;
\r
846 ("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
\r
847 return (int) GameUnfinished;
\r
850 [1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {
\r
852 if ((yyleng == 1) && (yytext[0] == '1'))
\r
853 return (int) MoveNumberOne;
\r
856 \([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
\r
857 /* elapsed time indication, e.g. (0:12) or {10:21.071} */
\r
858 return (int) ElapsedTime;
\r
862 /* position diagram enclosed in [-- --] */
\r
863 return (int) PositionDiagram;
\r
866 ^"{--------------"\n[^\}]*\n"--------------}"$ {
\r
867 /* position diagram enclosed in {-- --} */
\r
868 return (int) PositionDiagram;
\r
871 \[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
\r
872 return (int) PGNTag;
\r
875 [Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
\r
876 return (int) GNUChessGame;
\r
879 ^[#;%]" "[^ ]*(" game file"|" position file").*$ {
\r
880 return (int) XBoardGame;
\r
883 \$[0-9]+ { /* numeric annotation glyph */
\r
887 \{[^\}]*\} { /* anything in {} */
\r
888 return (int) Comment;
\r
891 ;.*$ { /* ; to end of line */
\r
892 return (int) Comment;
\r
895 \[[^\]]*\] { /* anything in [] */
\r
896 return (int) Comment;
\r
899 \([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */
\r
900 return (int) Comment;
\r
903 \([^)][^)]+\) { /* >=2 chars in () */
\r
904 return (int) Comment;
\r
907 ^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
\r
908 /* Skip mail headers */
\r
912 /* Skip random words */
\r
916 /* Skip everything else */
\r
922 static char *StringToLex;
\r
924 #ifndef FLEX_SCANNER
\r
925 static FILE *lexFP;
\r
931 if (StringToLex != NULL) {
\r
932 ret = *StringToLex;
\r
933 if (ret == NULLCHAR)
\r
937 } else if (unputCount > 0) {
\r
938 ret = unputBuffer[--unputCount];
\r
940 ret = fgetc(lexFP);
\r
950 * Return offset of next pattern within current file
\r
954 int offset = ftell(lexFP) - unputCount;
\r
962 static void output(ch)
\r
965 fprintf(stderr, "PARSER BUG: unmatched character '%c' (0%o)\n",
\r
969 static void unput(ch)
\r
972 if (ch == 0) return;
\r
973 if (StringToLex != NULL) {
\r
976 if (unputCount >= UNPUT_BUF_SIZE)
\r
977 fprintf(stderr, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
\r
979 unputBuffer[unputCount++] = ch;
\r
983 /* Get ready to lex from a new file. Kludge below sticks
\r
984 an artificial newline at the front of the file, which the
\r
985 above grammar ignores, but which makes ^ at start of pattern
\r
986 match at the real start of the file.
\r
992 StringToLex = NULL;
\r
994 unput('\n'); /* kludge */
\r
997 /* Get ready to lex from a string. ^ at start of pattern WON'T
\r
998 match at the start of the string!
\r
1007 #endif /*!FLEX_SCANNER*/
\r
1009 #ifdef FLEX_SCANNER
\r
1010 void my_yy_input(buf, result, max_size)
\r
1017 if (StringToLex != NULL) {
\r
1019 while (*StringToLex != NULLCHAR) {
\r
1020 *buf++ = *StringToLex++;
\r
1026 count = fread(buf, 1, max_size, yyin);
\r
1028 *result = YY_NULL;
\r
1036 static YY_BUFFER_STATE my_file_buffer = NULL;
\r
1039 Return offset of next pattern in the current file.
\r
1043 int pos = yy_c_buf_p - yy_current_buffer->yy_ch_buf;
\r
1045 return(ftell(yy_current_buffer->yy_input_file) -
\r
1046 yy_n_chars + pos);
\r
1053 if (my_file_buffer != NULL)
\r
1054 yy_delete_buffer(my_file_buffer);
\r
1056 my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1057 yy_switch_to_buffer(my_file_buffer);
\r
1063 if (my_file_buffer != NULL)
\r
1064 yy_delete_buffer(my_file_buffer);
\r
1065 StringToLex = NULL;
\r
1066 my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
\r
1067 yy_switch_to_buffer(my_file_buffer);
\r
1069 #endif /*FLEX_SCANNER*/
\r
1076 /* Parse a move from the given string s */
\r
1077 /* ^ at start of pattern WON'T work here unless using flex */
\r
1078 ChessMove yylexstr(boardIndex, s)
\r
1083 char *oldStringToLex;
\r
1084 #ifdef FLEX_SCANNER
\r
1085 YY_BUFFER_STATE buffer, oldBuffer;
\r
1088 yyboardindex = boardIndex;
\r
1089 oldStringToLex = StringToLex;
\r
1091 #ifdef FLEX_SCANNER
\r
1092 buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
\r
1093 oldBuffer = YY_CURRENT_BUFFER;
\r
1094 yy_switch_to_buffer(buffer);
\r
1095 #endif /*FLEX_SCANNER*/
\r
1097 ret = (ChessMove) yylex();
\r
1099 #ifdef FLEX_SCANNER
\r
1100 if (oldBuffer != NULL)
\r
1101 yy_switch_to_buffer(oldBuffer);
\r
1102 yy_delete_buffer(buffer);
\r
1103 #endif /*FLEX_SCANNER*/
\r
1104 StringToLex = oldStringToLex;
\r