-%a 10000
-%o 10000
-%e 2000
-%k 2500
-%p 7000
-%n 1000
-%{
-/*
- * parser.l -- lex parser of algebraic chess moves for XBoard
- * $Id$
- *
- * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
- * Enhancements Copyright 1992-95 Free Software Foundation, Inc.
- *
- * The following terms apply to Digital Equipment Corporation's copyright
- * interest in XBoard:
- * ------------------------------------------------------------------------
- * All Rights Reserved
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted,
- * provided that the above copyright notice appear in all copies and that
- * both that copyright notice and this permission notice appear in
- * supporting documentation, and that the name of Digital not be
- * used in advertising or publicity pertaining to distribution of the
- * software without specific, written prior permission.
- *
- * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
- * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
- * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
- * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
- * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
- * ------------------------------------------------------------------------
- *
- * The following terms apply to the enhanced version of XBoard distributed
- * by the Free Software Foundation:
- * ------------------------------------------------------------------------
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- * ------------------------------------------------------------------------
- */
-
-/* This parser handles all forms of promotion.
- * The parser resolves ambiguous moves by searching and check-testing.
- * It also parses comments of the form [anything] or (anything).
- */
-
-#include "config.h"
-
-#define NO_CONSTRAINT -1
-#undef YYLMAX
-#define YYLMAX 4096
-#define UNPUT_BUF_SIZE YYLMAX
-
-#ifdef FLEX_SCANNER
-/* yytext is probably a char*, but could be a char[]. yy_text is set
- in YY_DECL below, because if yytext is a char*, its value is not
- constant. */
-char *yy_text;
-#else /*!FLEX_SCANNER*/
-/* yytext is definitely a char[], so yy_text can be set here, statically. */
-char *yy_text = (char *) yytext;
-#endif
-
-#ifdef FLEX_SCANNER
-/* This is flex */
-/* use prototypes in function declarations */
-#define YY_USE_PROTOS
-
-#ifdef YY_USE_PROTOS
-#define YY_PROTO(proto) proto
-#else
-#define YY_PROTO(proto) ()
-#endif
-
-#undef YY_INPUT
-#define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size)
-#undef YY_DECL
-#define YY_DECL \
- int _yylex YY_PROTO((void)); \
- int yylex YY_PROTO((void)) \
- { \
- int result = _yylex(); \
- yy_text = (char *) yytext; \
- return(result); \
- } \
- int _yylex YY_PROTO((void))
-#else
-/* This is lex */
-#undef input
-#undef output
-#undef unput
-#endif
-
-/* The includes must be here, below the #undef input */
-
-#include <ctype.h>
-
-#if STDC_HEADERS
-# include <stdlib.h>
-# include <string.h>
-#else /* not STDC_HEADERS */
-# if HAVE_STRING_H
-# include <string.h>
-# else /* not HAVE_STRING_H */
-# include <strings.h>
-# endif /* not HAVE_STRING_H */
-#endif /* not STDC_HEADERS */
-
-#if HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
-#if defined(_amigados)
-# include <errno.h>
-# if HAVE_FCNTL_H
-# include <fcntl.h> /* isatty() prototype */
-# endif /* HAVE_FCNTL_H */
-#endif /* defined(_amigados) */
-
-#include "common.h"
-#include "backend.h"
-#include "frontend.h"
-#include "parser.h"
-#include "moves.h"
-
-extern int PosFlags P((int));
-
-extern Board boards[MAX_MOVES];
-int yyboardindex;
-int yyskipmoves = FALSE;
-char currentMoveString[YYLMAX];
-#ifndef FLEX_SCANNER
-char unputBuffer[UNPUT_BUF_SIZE];
-int unputCount = 0;
-#endif
-
-#ifdef FLEX_SCANNER
-void my_yy_input P((char *buf, int *result, int max_size));
-#else /*!FLEX_SCANNER*/
-static int input P((void));
-static void output P((int ch));
-static void unput P((int ch));
-int yylook P((void));
-int yyback P((int *, int));
-#endif
-#undef yywrap
-int yywrap P((void));
-extern void CopyBoard P((Board to, Board from));
-
-%}
-%%
-
-[RrBbNnQqKkPp][/]?[a-h][1-8][xX:-]?[a-h][1-8](=?\(?[RrBbNnQqKk]\)?)? {
- /*
- * Fully-qualified algebraic move, possibly with promotion
- */
- int skip1 = 0, skip2 = 0;
- ChessSquare piece;
- ChessMove result;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove the / */
- if (yytext[1] == '/') skip1 = 1;
-
- /* remove the [xX:-] */
- if ((yytext[3+skip1] == 'x') || (yytext[3+skip1] == 'X') ||
- (yytext[3+skip1] == '-') || (yytext[3+skip1] == ':')) skip2 = 1;
-
- currentMoveString[0] = yytext[1+skip1];
- currentMoveString[1] = yytext[2+skip1];
- currentMoveString[2] = yytext[3+skip1+skip2];
- currentMoveString[3] = yytext[4+skip1+skip2];
- currentMoveString[4] = NULLCHAR;
-
- if (yyleng-skip1-skip2 > 5) {
- if (yytext[yyleng-1] == ')') {
- currentMoveString[4] = ToLower(yytext[yyleng-2]);
- } else {
- currentMoveString[4] = ToLower(yytext[yyleng-1]);
- }
- currentMoveString[5] = NULLCHAR;
- }
-
- piece = boards[yyboardindex]
- [currentMoveString[1] - '1'][currentMoveString[0] - 'a'];
- if (ToLower(yytext[0]) != ToLower(PieceToChar(piece)))
- return (int) IllegalMove;
-
- result = LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- currentMoveString[1] - '1',
- currentMoveString[0] - 'a',
- currentMoveString[3] - '1',
- currentMoveString[2] - 'a',
- currentMoveString[4]);
-
- if (currentMoveString[4] == NULLCHAR &&
- (result == WhitePromotionQueen || result == BlackPromotionQueen)) {
- currentMoveString[4] = 'q';
- currentMoveString[5] = NULLCHAR;
- }
-
- return (int) result;
-}
-
-[a-h][1-8][xX:-]?[a-h][1-8](=?\(?[RrBbNnQqKk]\)?)? {
- /*
- * Simple algebraic move, possibly with promotion
- */
- int skip = 0;
- ChessMove result;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove the [xX:-] */
- if ((yytext[2] == 'x') || (yytext[2] == 'X') ||
- (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;
-
- currentMoveString[0] = yytext[0];
- currentMoveString[1] = yytext[1];
- currentMoveString[2] = yytext[2+skip];
- currentMoveString[3] = yytext[3+skip];
- currentMoveString[4] = NULLCHAR;
-
- if (yyleng-skip > 4) {
- if (yytext[yyleng-1] == ')') {
- currentMoveString[4] = ToLower(yytext[yyleng-2]);
- } else {
- currentMoveString[4] = ToLower(yytext[yyleng-1]);
- }
- currentMoveString[5] = NULLCHAR;
- }
-
- result = LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- currentMoveString[1] - '1',
- currentMoveString[0] - 'a',
- currentMoveString[3] - '1',
- currentMoveString[2] - 'a',
- currentMoveString[4]);
-
- if (currentMoveString[4] == NULLCHAR &&
- (result == WhitePromotionQueen || result == BlackPromotionQueen)) {
- currentMoveString[4] = 'q';
- currentMoveString[5] = NULLCHAR;
- }
-
- return (int) result;
-}
-
-[a-h][1-8](=?\(?[RrBbNnQqKk]\)?)? {
- /*
- * Pawn move, possibly with promotion
- */
- DisambiguateClosure cl;
- int skip = 0;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove the =() */
- if (yytext[2] == '=') skip++;
- if (yytext[2+skip] == '(') skip++;
-
- cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
- cl.rfIn = -1;
- cl.ffIn = yytext[0] - 'a';
- cl.rtIn = yytext[1] - '1';
- cl.ftIn = yytext[0] - 'a';
- cl.promoCharIn = yytext[2+skip];
- Disambiguate(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);
-
- currentMoveString[0] = cl.ff + 'a';
- currentMoveString[1] = cl.rf + '1';
- currentMoveString[2] = cl.ft + 'a';
- currentMoveString[3] = cl.rt + '1';
- currentMoveString[4] = cl.promoChar;
- currentMoveString[5] = NULLCHAR;
-
- return (int) cl.kind;
-}
-
-
-(ab|bc|cd|de|ef|fg|gh|hg|gf|fe|ed|dc|cb|ba|([a-h][xX:-][a-h]))(=?\(?[RrBbNnQqKk]\)?)?(ep|"e.p.")? {
- /*
- * Pawn capture, possibly with promotion, possibly ambiguous
- */
- DisambiguateClosure cl;
- int skip1 = 0, skip2 = 0;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove trailing ep or e.p. (nonstandard PGN) */
- if (yytext[yyleng-1] == 'p') {
- yyleng -= 2;
- yytext[yyleng] = NULLCHAR;
- } else if (yytext[yyleng-1] == '.') {
- yyleng -= 4;
- yytext[yyleng] = NULLCHAR;
- }
-
- /* remove the [xX:-] and =() */
- if ((yytext[1] == 'x') || (yytext[1] == 'X')
- || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;
- if (yytext[2+skip1] == '=') skip2++;
- if (yytext[2+skip1+skip2] == '(') skip2++;
-
- cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;
- cl.rfIn = -1;
- cl.ffIn = yytext[0] - 'a';
- cl.rtIn = -1;
- cl.ftIn = yytext[1+skip1] - 'a';
- cl.promoCharIn = yytext[2+skip1+skip2];
- Disambiguate(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);
-
- currentMoveString[0] = cl.ff + 'a';
- currentMoveString[1] = cl.rf + '1';
- currentMoveString[2] = cl.ft + 'a';
- currentMoveString[3] = cl.rt + '1';
- currentMoveString[4] = cl.promoChar;
- currentMoveString[5] = NULLCHAR;
-
- return (int) cl.kind;
-}
-
-[a-h][xX:]?[a-h][1-8](=?\(?[RrBbNnQqKk]\)?)?(ep|"e.p.")? {
- /*
- * unambiguously abbreviated Pawn capture, possibly with promotion
- */
- int skip = 0;
- ChessMove result;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove trailing ep or e.p. (nonstandard PGN) */
- if (yytext[yyleng-1] == 'p') {
- yyleng -= 2;
- yytext[yyleng] = NULLCHAR;
- } else if (yytext[yyleng-1] == '.') {
- yyleng -= 4;
- yytext[yyleng] = NULLCHAR;
- }
-
- /* remove the [xX:-] */
- if ((yytext[1] == 'x') || (yytext[1] == 'X')
- || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
-
- currentMoveString[0] = yytext[0];
- currentMoveString[2] = yytext[1+skip];
- currentMoveString[3] = yytext[2+skip];
- if (WhiteOnMove(yyboardindex)) {
- if (yytext[2+skip] == '1') return (int) ImpossibleMove;
- currentMoveString[1] = yytext[2+skip] - 1;
- } else {
- if (yytext[2+skip] == '8') return (int) ImpossibleMove;
- currentMoveString[1] = yytext[2+skip] + 1;
- }
- if (yyleng-skip > 3) {
- if (yytext[yyleng-1] == ')')
- currentMoveString[4] = ToLower(yytext[yyleng-2]);
- else
- currentMoveString[4] = ToLower(yytext[yyleng-1]);
- currentMoveString[5] = NULLCHAR;
- } else {
- currentMoveString[4] = NULLCHAR;
- }
-
- result = LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- currentMoveString[1] - '1',
- currentMoveString[0] - 'a',
- currentMoveString[3] - '1',
- currentMoveString[2] - 'a',
- currentMoveString[4]);
-
- if (currentMoveString[4] == NULLCHAR &&
- (result == WhitePromotionQueen || result == BlackPromotionQueen)) {
- currentMoveString[4] = 'q';
- currentMoveString[5] = NULLCHAR;
- }
-
- if (result != IllegalMove) return (int) result;
-
- /* Special case: improperly written en passant capture */
- if (WhiteOnMove(yyboardindex)) {
- if (currentMoveString[3] == '5') {
- currentMoveString[1] = '5';
- currentMoveString[3] = '6';
- } else {
- return (int) IllegalMove;
- }
- } else {
- if (currentMoveString[3] == '4') {
- currentMoveString[1] = '4';
- currentMoveString[3] = '3';
- } else {
- return (int) IllegalMove;
- }
- }
-
- result = LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- currentMoveString[1] - '1',
- currentMoveString[0] - 'a',
- currentMoveString[3] - '1',
- currentMoveString[2] - 'a',
- currentMoveString[4]);
-
- if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)
- return (int) result;
- else
- return (int) IllegalMove;
-}
-
-[RrBbNnQqKk][xX:-]?[a-h][1-8] {
- /*
- * piece move, possibly ambiguous
- */
- DisambiguateClosure cl;
- int skip = 0;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove the [xX:-] */
- if ((yytext[1] == 'x') || (yytext[1] == 'X')
- || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;
-
- if (WhiteOnMove(yyboardindex)) {
- cl.pieceIn = CharToPiece(ToUpper(yytext[0]));
- } else {
- cl.pieceIn = CharToPiece(ToLower(yytext[0]));
- }
- cl.rfIn = -1;
- cl.ffIn = -1;
- cl.rtIn = yytext[2+skip] - '1';
- cl.ftIn = yytext[1+skip] - 'a';
- cl.promoCharIn = NULLCHAR;
- Disambiguate(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);
-
- currentMoveString[0] = cl.ff + 'a';
- currentMoveString[1] = cl.rf + '1';
- currentMoveString[2] = cl.ft + 'a';
- currentMoveString[3] = cl.rt + '1';
- currentMoveString[4] = cl.promoChar;
- currentMoveString[5] = NULLCHAR;
-
- return (int) cl.kind;
-}
-
-[RrBbNnQqKk][a-h1-8][xX:-]?[a-h][1-8] {
- /*
- * piece move with rank or file disambiguator
- */
- DisambiguateClosure cl;
- int skip = 0;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- /* remove the [xX:-] */
- if ((yytext[2] == 'x') || (yytext[2] == 'X')
- || (yytext[2] == ':') || (yytext[2] == '-')) skip = 1;
-
- if (WhiteOnMove(yyboardindex)) {
- cl.pieceIn = CharToPiece(ToUpper(yytext[0]));
- } else {
- cl.pieceIn = CharToPiece(ToLower(yytext[0]));
- }
- if (isalpha(yytext[1])) {
- cl.rfIn = -1;
- cl.ffIn = yytext[1] - 'a';
- } else {
- cl.rfIn = yytext[1] - '1';
- cl.ffIn = -1;
- }
- cl.rtIn = yytext[3+skip] - '1';
- cl.ftIn = yytext[2+skip] - 'a';
- cl.promoCharIn = NULLCHAR;
- Disambiguate(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);
-
- currentMoveString[0] = cl.ff + 'a';
- currentMoveString[1] = cl.rf + '1';
- currentMoveString[2] = cl.ft + 'a';
- currentMoveString[3] = cl.rt + '1';
- currentMoveString[4] = cl.promoChar;
- currentMoveString[5] = NULLCHAR;
-
- return (int) cl.kind;
-}
-
-000|0-0-0|ooo|OOO|o-o-o|O-O-O {
- int rf, ff, rt, ft;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- if (WhiteOnMove(yyboardindex)) {
- if (boards[yyboardindex][0][3] == WhiteKing) {
- /* ICS wild castling */
- strcpy(currentMoveString, "d1f1");
- rf = 0;
- ff = 3;
- rt = 0;
- ft = 5;
- } else {
- strcpy(currentMoveString, "e1c1");
- rf = 0;
- ff = 4;
- rt = 0;
- ft = 2;
- }
- } else{
- if (boards[yyboardindex][7][3] == BlackKing) {
- /* ICS wild castling */
- strcpy(currentMoveString, "d8f8");
- rf = 7;
- ff = 3;
- rt = 7;
- ft = 5;
- } else {
- strcpy(currentMoveString, "e8c8");
- rf = 7;
- ff = 4;
- rt = 7;
- ft = 2;
- }
- }
- return (int) LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- rf, ff, rt, ft, NULLCHAR);
-}
-
-00|0-0|oo|OO|o-o|O-O {
- int rf, ff, rt, ft;
-
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
-
- if (WhiteOnMove(yyboardindex)) {
- if (boards[yyboardindex][0][3] == WhiteKing) {
- /* ICS wild castling */
- strcpy(currentMoveString, "d1b1");
- rf = 0;
- ff = 3;
- rt = 0;
- ft = 1;
- } else {
- strcpy(currentMoveString, "e1g1");
- rf = 0;
- ff = 4;
- rt = 0;
- ft = 6;
- }
- } else {
- if (boards[yyboardindex][7][3] == BlackKing) {
- /* ICS wild castling */
- strcpy(currentMoveString, "d8b8");
- rf = 7;
- ff = 3;
- rt = 7;
- ft = 1;
- } else {
- strcpy(currentMoveString, "e8g8");
- rf = 7;
- ff = 4;
- rt = 7;
- ft = 6;
- }
- }
- return (int) LegalityTest(boards[yyboardindex],
- PosFlags(yyboardindex), EP_UNKNOWN,
- rf, ff, rt, ft, NULLCHAR);
-}
-
-[PpNnBbRrQq]@[a-h][1-8] {
- /* Bughouse piece drop. No legality checking for now. */
- currentMoveString[1] = '@';
- currentMoveString[2] = yytext[2];
- currentMoveString[3] = yytext[3];
- currentMoveString[4] = NULLCHAR;
- if (WhiteOnMove(yyboardindex)) {
- currentMoveString[0] = ToUpper(yytext[0]);
- return (int) WhiteDrop;
- } else {
- currentMoveString[0] = ToLower(yytext[0]);
- return (int) BlackDrop;
- }
-}
-
-[Rr]esign(s|ed)? {
- if (WhiteOnMove(yyboardindex))
- return (int) BlackWins;
- else
- return (int) WhiteWins;
-}
-
-(([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {
- return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
-}
-
-(([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {
- return (int) GameUnfinished;
-}
-
-[Ss]talemate {
- return (int) GameIsDrawn;
-}
-
-"+-+" {
- return (int) GameIsDrawn;
-}
-
-([Cc]heck)?[Mm]ate {
- if (WhiteOnMove(yyboardindex))
- return (int) BlackWins;
- else
- return (int) WhiteWins;
-}
-
-"++" {
- if (WhiteOnMove(yyboardindex))
- return (int) BlackWins;
- else
- return (int) WhiteWins;
-}
-
-[Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {
- return (int) GameIsDrawn;
-}
-
-[Dd]raw(n)?(" (".*")")? {
- return (int) GameIsDrawn;
-}
-
-(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ww][io]n(s)?.*) {
- return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);
-}
-
-(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ll]os[tes]+.*) {
- return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
-}
-
-("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? {
- return (int) WhiteWins;
-}
-
-("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? {
- return (int) BlackWins;
-}
-
-("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
- return (int) GameIsDrawn;
-}
-
-("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
- return (int) GameUnfinished;
-}
-
-[1-9][0-9]*/"."?[ \t\n]*[a-hNnPpRrBQqKkOo] {
- /* move numbers */
- if ((yyleng == 1) && (yytext[0] == '1'))
- return (int) MoveNumberOne;
-}
-
-\([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {
- /* elapsed time indication, e.g. (0:12) or {10:21.071} */
- return (int) ElapsedTime;
-}
-
-"[--"[^\]]*"--]" {
- /* position diagram enclosed in [-- --] */
- return (int) PositionDiagram;
-}
-
-^"{--------------"\n[^\}]*\n"--------------}"$ {
- /* position diagram enclosed in {-- --} */
- return (int) PositionDiagram;
-}
-
-\[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {
- return (int) PGNTag;
-}
-
-[Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {
- return (int) GNUChessGame;
-}
-
-^[#;%]" "[^ ]*(" game file"|" position file").*$ {
- return (int) XBoardGame;
-}
-
-\$[0-9]+ { /* numeric annotation glyph */
- return (int) NAG;
-}
-
-\{[^\}]*\} { /* anything in {} */
- return (int) Comment;
-}
-
-;.*$ { /* ; to end of line */
- return (int) Comment;
-}
-
-\[[^\]]*\] { /* anything in [] */
- return (int) Comment;
-}
-
-\([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */
- return (int) Comment;
-}
-
-\([^)][^)]+\) { /* >=2 chars in () */
- return (int) Comment;
-}
-
-^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {
- /* Skip mail headers */
-}
-
-[a-zA-Z0-9'-]+ {
- /* Skip random words */
-}
-
-.|\n {
- /* Skip everything else */
-}
-
-%%
-
-
-static char *StringToLex;
-
-#ifndef FLEX_SCANNER
-static FILE *lexFP;
-
-static int input()
-{
- int ret;
-
- if (StringToLex != NULL) {
- ret = *StringToLex;
- if (ret == NULLCHAR)
- ret = EOF;
- else
- StringToLex++;
- } else if (unputCount > 0) {
- ret = unputBuffer[--unputCount];
- } else {
- ret = fgetc(lexFP);
- }
-
- if (ret == EOF)
- return 0;
- else
- return ret;
-}
-
-/*
- * Return offset of next pattern within current file
- */
-int yyoffset()
-{
- int offset = ftell(lexFP) - unputCount;
-
- if (offset < 0) {
- offset = 0;
- }
- return(offset);
-}
-
-static void output(ch)
- int ch;
-{
- fprintf(stderr, "PARSER BUG: unmatched character '%c' (0%o)\n",
- ch, ch);
-}
-
-static void unput(ch)
- int ch;
-{
- if (ch == 0) return;
- if (StringToLex != NULL) {
- StringToLex--;
- } else {
- if (unputCount >= UNPUT_BUF_SIZE)
- fprintf(stderr, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",
- ch, ch);
- unputBuffer[unputCount++] = ch;
- }
-}
-
-/* Get ready to lex from a new file. Kludge below sticks
- an artificial newline at the front of the file, which the
- above grammar ignores, but which makes ^ at start of pattern
- match at the real start of the file.
-*/
-void yynewfile(f)
- FILE *f;
-{
- lexFP = f;
- StringToLex = NULL;
- unputCount = 0;
- unput('\n'); /* kludge */
-}
-
-/* Get ready to lex from a string. ^ at start of pattern WON'T
- match at the start of the string!
-*/
-void yynewstr(s)
- char *s;
-{
- lexFP = NULL;
- StringToLex = s;
- unputCount = 0;
-}
-#endif /*!FLEX_SCANNER*/
-
-#ifdef FLEX_SCANNER
-void my_yy_input(buf, result, max_size)
- char *buf;
- int *result;
- int max_size;
-{
- int count;
-
- if (StringToLex != NULL) {
- count = 0;
- while (*StringToLex != NULLCHAR) {
- *buf++ = *StringToLex++;
- count++;
- }
- *result = count;
- return;
- } else {
- count = fread(buf, 1, max_size, yyin);
- if (count == 0) {
- *result = YY_NULL;
- } else {
- *result = count;
- }
- return;
- }
-}
-
-static YY_BUFFER_STATE my_file_buffer = NULL;
-
-/*
- Return offset of next pattern in the current file.
-*/
-int yyoffset()
-{
- int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;
-
- return(ftell(YY_CURRENT_BUFFER->yy_input_file) -
- yy_n_chars + pos);
-}
-
-
-void yynewstr(s)
- char *s;
-{
- if (my_file_buffer != NULL)
- yy_delete_buffer(my_file_buffer);
- StringToLex = s;
- my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
- yy_switch_to_buffer(my_file_buffer);
-}
-
-void yynewfile(f)
- FILE *f;
-{
- if (my_file_buffer != NULL)
- yy_delete_buffer(my_file_buffer);
- StringToLex = NULL;
- my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);
- yy_switch_to_buffer(my_file_buffer);
-}
-#endif /*FLEX_SCANNER*/
-
-int yywrap()
-{
- return TRUE;
-}
-
-/* Parse a move from the given string s */
-/* ^ at start of pattern WON'T work here unless using flex */
-ChessMove yylexstr(boardIndex, s)
- int boardIndex;
- char *s;
-{
- ChessMove ret;
- char *oldStringToLex;
-#ifdef FLEX_SCANNER
- YY_BUFFER_STATE buffer, oldBuffer;
-#endif
-
- yyboardindex = boardIndex;
- oldStringToLex = StringToLex;
- StringToLex = s;
-#ifdef FLEX_SCANNER
- buffer = yy_create_buffer(stdin, YY_BUF_SIZE);
- oldBuffer = YY_CURRENT_BUFFER;
- yy_switch_to_buffer(buffer);
-#endif /*FLEX_SCANNER*/
-
- ret = (ChessMove) yylex();
-
-#ifdef FLEX_SCANNER
- if (oldBuffer != NULL)
- yy_switch_to_buffer(oldBuffer);
- yy_delete_buffer(buffer);
-#endif /*FLEX_SCANNER*/
- StringToLex = oldStringToLex;
-
- return ret;
-}
+%a 10000\r
+%o 10000\r
+%e 2000\r
+%k 2500\r
+%p 7000\r
+%n 1000\r
+%{\r
+/*\r
+ * parser.l -- lex parser of algebraic chess moves for XBoard\r
+ *\r
+ * Copyright 1991 by Digital Equipment Corporation, Maynard,\r
+ * Massachusetts.\r
+ *\r
+ * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005,\r
+ * 2006, 2007, 2008, 2009 Free Software Foundation, Inc.\r
+ *\r
+ * The following terms apply to Digital Equipment Corporation's copyright\r
+ * interest in XBoard:\r
+ * ------------------------------------------------------------------------\r
+ * All Rights Reserved\r
+ *\r
+ * Permission to use, copy, modify, and distribute this software and its\r
+ * documentation for any purpose and without fee is hereby granted,\r
+ * provided that the above copyright notice appear in all copies and that\r
+ * both that copyright notice and this permission notice appear in\r
+ * supporting documentation, and that the name of Digital not be\r
+ * used in advertising or publicity pertaining to distribution of the\r
+ * software without specific, written prior permission.\r
+ *\r
+ * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING\r
+ * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL\r
+ * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR\r
+ * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\r
+ * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,\r
+ * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
+ * SOFTWARE.\r
+ * ------------------------------------------------------------------------\r
+ *\r
+ * The following terms apply to the enhanced version of XBoard\r
+ * distributed by the Free Software Foundation:\r
+ * ------------------------------------------------------------------------\r
+ *\r
+ * GNU XBoard is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or (at\r
+ * your option) any later version.\r
+ *\r
+ * GNU XBoard is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see http://www.gnu.org/licenses/. \r
+ *\r
+ *------------------------------------------------------------------------\r
+ ** See the file ChangeLog for a revision history. */\r
+\r
+/* This parser handles all forms of promotion.\r
+ * The parser resolves ambiguous moves by searching and check-testing.\r
+ * It also parses comments of the form [anything] or (anything).\r
+ *\r
+ * [HGM] Parser extensively modified for bigger boards, Shogi-like syntax,\r
+ * and unknow pieces. All pieces are now mandatory upper case, but can be\r
+ * any letter A-Z. Files must be lower case (as before), but can run upto 'l'.\r
+ * Ranks can be 0-9. The parser returns 0 for off-board files and ranks.\r
+ * For an unknown piece (as mover or promotion piece) it returns\r
+ * IllegalMove, like it does when the piece doesn't match.\r
+ * Promotions can now also be appended Shogi-style, a bare '=' or '+',\r
+ * and this is then returned as promotion character. The piece indicator\r
+ * can be prefixed by a '+' to indicate it is a promoted piece.\r
+ */\r
+\r
+#include "config.h"\r
+\r
+#define NO_CONSTRAINT -1\r
+#undef YYLMAX\r
+#define YYLMAX 4096\r
+#define UNPUT_BUF_SIZE YYLMAX\r
+\r
+#ifdef FLEX_SCANNER\r
+/* yytext is probably a char*, but could be a char[]. yy_text is set\r
+ in YY_DECL below, because if yytext is a char*, its value is not\r
+ constant. */\r
+char *yy_text;\r
+#else /*!FLEX_SCANNER*/\r
+/* yytext is definitely a char[], so yy_text can be set here, statically. */\r
+char *yy_text = (char *) yytext;\r
+#endif\r
+\r
+#ifdef FLEX_SCANNER\r
+/* This is flex */\r
+/* [AP] use prototypes in function declarations */\r
+#define YY_USE_PROTOS\r
+\r
+#ifdef YY_USE_PROTOS\r
+#define YY_PROTO(proto) proto\r
+#else\r
+#define YY_PROTO(proto) ()\r
+#endif\r
+/* end of [AP] fix */\r
+\r
+#undef YY_INPUT\r
+#define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size)\r
+#undef YY_DECL\r
+#define YY_DECL \\r
+ int _yylex YY_PROTO((void)); \\r
+ int yylex YY_PROTO((void)) \\r
+ { \\r
+ int result = _yylex(); \\r
+ yy_text = (char *) yytext; \\r
+ return(result); \\r
+ } \\r
+ int _yylex YY_PROTO((void))\r
+#else\r
+/* This is lex */\r
+#undef input\r
+#undef output\r
+#undef unput\r
+#endif\r
+\r
+/* The includes must be here, below the #undef input */\r
+\r
+#include <ctype.h>\r
+\r
+#if STDC_HEADERS\r
+# include <stdlib.h>\r
+# include <string.h>\r
+#else /* not STDC_HEADERS */\r
+# if HAVE_STRING_H\r
+# include <string.h>\r
+# else /* not HAVE_STRING_H */\r
+# include <strings.h>\r
+# endif /* not HAVE_STRING_H */\r
+#endif /* not STDC_HEADERS */\r
+\r
+#if HAVE_UNISTD_H\r
+# include <unistd.h>\r
+#endif\r
+\r
+#if defined(_amigados)\r
+# include <errno.h>\r
+# if HAVE_FCNTL_H\r
+# include <fcntl.h> /* isatty() prototype */\r
+# endif /* HAVE_FCNTL_H */\r
+#endif /* defined(_amigados) */\r
+\r
+#include "common.h"\r
+#include "backend.h"\r
+#include "frontend.h"\r
+#include "parser.h"\r
+#include "moves.h"\r
+\r
+extern int PosFlags P((int));\r
+\r
+extern Board boards[MAX_MOVES];\r
+int yyboardindex;\r
+int yyskipmoves = FALSE;\r
+char currentMoveString[YYLMAX];\r
+#ifndef FLEX_SCANNER\r
+char unputBuffer[UNPUT_BUF_SIZE];\r
+int unputCount = 0;\r
+#endif\r
+\r
+#ifdef FLEX_SCANNER\r
+void my_yy_input P((char *buf, int *result, int max_size));\r
+#else /*!FLEX_SCANNER*/\r
+static int input P((void));\r
+static void output P((int ch));\r
+static void unput P((int ch));\r
+int yylook P((void));\r
+int yyback P((int *, int));\r
+#endif\r
+#undef yywrap\r
+int yywrap P((void));\r
+extern void CopyBoard P((Board to, Board from));\r
+\r
+%}\r
+%%\r
+\r
+"+"?[A-Z][/]?[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {\r
+ /*\r
+ * Fully-qualified algebraic move, possibly with promotion\r
+ */\r
+ int skip1 = 0, skip2 = 0, skip3 = 0, promoted = 0;\r
+ ChessSquare piece;\r
+ ChessMove result;\r
+ char c;\r
+ \r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ if (yytext[0] == '+') skip1 = skip3 = promoted = 1; /* [HGM] Shogi promoted */\r
+\r
+ /* remove the / */\r
+ if (yytext[1+skip1] == '/') skip1++; \r
+ \r
+ /* remove the [xX:-] */\r
+ if ((yytext[3+skip1] == 'x') || (yytext[3+skip1] == 'X') ||\r
+ (yytext[3+skip1] == '-') || (yytext[3+skip1] == ':')) skip2 = 1;\r
+ \r
+ currentMoveString[0] = yytext[1+skip1];\r
+ currentMoveString[1] = yytext[2+skip1];\r
+ currentMoveString[2] = yytext[3+skip1+skip2];\r
+ currentMoveString[3] = yytext[4+skip1+skip2];\r
+ currentMoveString[4] = NULLCHAR;\r
+ \r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "Parser Qa1b2: yyleng=%d\n",\r
+ yyleng);\r
+ }\r
+\r
+ if (yyleng-skip1-skip2 > 5) { char c;\r
+ if (yytext[yyleng-1] == ')') {\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-2]);\r
+ } else {\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-1]);\r
+ }\r
+ currentMoveString[5] = NULLCHAR;\r
+ if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)\r
+ return IllegalMove; /* [HGM] promotion to invalid piece */\r
+ }\r
+\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "parser: %s\n", currentMoveString);\r
+ }\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[1] - ONE < 0 ||\r
+ currentMoveString[0] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[3] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[3] - ONE < 0 ||\r
+ currentMoveString[2] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[0] - AAA < BOARD_LEFT ||\r
+ currentMoveString[2] - AAA < BOARD_LEFT )\r
+ return 0;\r
+\r
+ piece = boards[yyboardindex]\r
+ [currentMoveString[1] - ONE][currentMoveString[0] - AAA];\r
+ if(promoted) piece = (ChessSquare) (DEMOTED piece);\r
+ c = PieceToChar(piece);\r
+ if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));\r
+ if (ToLower(yytext[skip3]) != ToLower(c))\r
+ return (int) IllegalMove;\r
+\r
+ result = LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+ EP_UNKNOWN,\r
+ initialRights, /* [HGM] assume all castlings allowed */\r
+ currentMoveString[1] - ONE,\r
+ currentMoveString[0] - AAA,\r
+ currentMoveString[3] - ONE,\r
+ currentMoveString[2] - AAA,\r
+ currentMoveString[4]);\r
+\r
+ if (currentMoveString[4] == NULLCHAR &&\r
+ (result == WhitePromotionKnight || result == BlackPromotionKnight ||\r
+ result == WhitePromotionQueen || result == BlackPromotionQueen)) {\r
+ currentMoveString[4] = PieceToChar(BlackQueen);\r
+ currentMoveString[5] = NULLCHAR;\r
+ }\r
+\r
+ return (int) result;\r
+}\r
+\r
+[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {\r
+ /*\r
+ * Simple algebraic move, possibly with promotion\r
+ * [HGM] Engine moves are received in this format, with lower-case promoChar!\r
+ */\r
+ int skip = 0;\r
+ ChessMove result;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ /* remove the [xX:-] */\r
+ if ((yytext[2] == 'x') || (yytext[2] == 'X') ||\r
+ (yytext[2] == '-') || (yytext[2] == ':')) skip = 1;\r
+\r
+ currentMoveString[0] = yytext[0];\r
+ currentMoveString[1] = yytext[1];\r
+ currentMoveString[2] = yytext[2+skip];\r
+ currentMoveString[3] = yytext[3+skip];\r
+ currentMoveString[4] = NULLCHAR;\r
+\r
+ if (yyleng-skip > 4) { char c;\r
+ if (yytext[yyleng-1] == ')') {\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-2]);\r
+ } else {\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-1]);\r
+ }\r
+ currentMoveString[5] = NULLCHAR;\r
+ if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)\r
+ return IllegalMove;\r
+ }\r
+\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(currentMoveString[1] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[1] - ONE < 0 ||\r
+ currentMoveString[0] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[3] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[3] - ONE < 0 ||\r
+ currentMoveString[2] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[0] - AAA < BOARD_LEFT ||\r
+ currentMoveString[2] - AAA < BOARD_LEFT )\r
+ return 0;\r
+\r
+ result = LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+ EP_UNKNOWN,\r
+ initialRights, /* [HGM] assume all castlings allowed */\r
+ currentMoveString[1] - ONE,\r
+ currentMoveString[0] - AAA,\r
+ currentMoveString[3] - ONE,\r
+ currentMoveString[2] - AAA,\r
+ currentMoveString[4]);\r
+\r
+ if (currentMoveString[4] == NULLCHAR &&\r
+ (result == WhitePromotionKnight || result == BlackPromotionKnight ||\r
+ result == WhitePromotionQueen || result == BlackPromotionQueen)) {\r
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier)\r
+ currentMoveString[4] = PieceToChar(BlackFerz);\r
+ else if(gameInfo.variant == VariantGreat)\r
+ currentMoveString[4] = PieceToChar(BlackMan);\r
+ else\r
+ currentMoveString[4] = PieceToChar(BlackQueen);\r
+ currentMoveString[5] = NULLCHAR;\r
+ }\r
+\r
+ return (int) result;\r
+}\r
+\r
+[a-l][0-9]((=?\(?[A-Z]\)?)|=)? {\r
+ /*\r
+ * Pawn move, possibly with promotion\r
+ */\r
+ DisambiguateClosure cl;\r
+ int skip = 0; char c;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ /* remove the =() */\r
+ if (yytext[2] == '=' && yytext[3] != NULLCHAR) skip++;\r
+ if (yytext[2+skip] == '(') skip++;\r
+\r
+ cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;\r
+ cl.rfIn = -1;\r
+ cl.ffIn = yytext[0] - AAA;\r
+ cl.rtIn = yytext[1] - ONE;\r
+ cl.ftIn = yytext[0] - AAA;\r
+ c = cl.promoCharIn = yytext[2+skip];\r
+\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(cl.rtIn >= BOARD_HEIGHT ||\r
+ cl.rtIn < 0 ||\r
+ cl.ffIn >= BOARD_RGHT ||\r
+ cl.ftIn < BOARD_LEFT )\r
+ return 0;\r
+\r
+ if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)\r
+ return IllegalMove;\r
+\r
+\r
+ Disambiguate(boards[yyboardindex],\r
+ PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+\r
+ currentMoveString[0] = cl.ff + AAA;\r
+ currentMoveString[1] = cl.rf + ONE;\r
+ currentMoveString[2] = cl.ft + AAA;\r
+ currentMoveString[3] = cl.rt + ONE;\r
+ currentMoveString[4] = cl.promoChar;\r
+ currentMoveString[5] = NULLCHAR;\r
+\r
+ return (int) cl.kind;\r
+}\r
+\r
+\r
+(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
+ /*\r
+ * Pawn capture, possibly with promotion, possibly ambiguous\r
+ */\r
+ DisambiguateClosure cl;\r
+ int skip1 = 0, skip2 = 0; char c;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ /* remove trailing ep or e.p. (nonstandard PGN) */\r
+ if (yytext[yyleng-1] == 'p') {\r
+ yyleng -= 2;\r
+ yytext[yyleng] = NULLCHAR;\r
+ } else if (yytext[yyleng-1] == '.') {\r
+ yyleng -= 4;\r
+ yytext[yyleng] = NULLCHAR;\r
+ }\r
+\r
+ /* remove the [xX:-] and =() */\r
+ if ((yytext[1] == 'x') || (yytext[1] == 'X')\r
+ || (yytext[1] == ':') || (yytext[1] == '-')) skip1 = 1;\r
+ if (yytext[2+skip1] == '=' && yytext[3+skip1] != NULLCHAR) skip2++;\r
+ if (yytext[2+skip1+skip2] == '(') skip2++;\r
+\r
+ cl.pieceIn = WhiteOnMove(yyboardindex) ? WhitePawn : BlackPawn;\r
+ cl.rfIn = -1;\r
+ cl.ffIn = yytext[0] - AAA;\r
+ cl.rtIn = -1;\r
+ cl.ftIn = yytext[1+skip1] - AAA;\r
+ c = cl.promoCharIn = yytext[2+skip1+skip2];\r
+\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(cl.ffIn >= BOARD_RGHT ||\r
+ cl.ffIn < BOARD_LEFT ||\r
+ cl.ftIn >= BOARD_RGHT ||\r
+ cl.ftIn < BOARD_LEFT )\r
+ return 0;\r
+\r
+ if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare)\r
+ return IllegalMove;\r
+\r
+ Disambiguate(boards[yyboardindex],\r
+ PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+\r
+ currentMoveString[0] = cl.ff + AAA;\r
+ currentMoveString[1] = cl.rf + ONE;\r
+ currentMoveString[2] = cl.ft + AAA;\r
+ currentMoveString[3] = cl.rt + ONE;\r
+ currentMoveString[4] = cl.promoChar;\r
+ currentMoveString[5] = NULLCHAR;\r
+\r
+ return (int) cl.kind;\r
+}\r
+\r
+[a-l][xX:]?[a-l][0-9]((=?\(?[A-Z]\)?)|ep|"e.p."|=)? {\r
+ /*\r
+ * unambiguously abbreviated Pawn capture, possibly with promotion\r
+ */\r
+ int skip = 0;\r
+ ChessMove result; char c;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ /* remove trailing ep or e.p. (nonstandard PGN) */\r
+ if (yytext[yyleng-1] == 'p') {\r
+ yyleng -= 2;\r
+ yytext[yyleng] = NULLCHAR;\r
+ } else if (yytext[yyleng-1] == '.') {\r
+ yyleng -= 4;\r
+ yytext[yyleng] = NULLCHAR;\r
+ }\r
+\r
+ /* remove the [xX:-] */\r
+ if ((yytext[1] == 'x') || (yytext[1] == 'X')\r
+ || (yytext[1] == ':') || (yytext[1] == '-')) skip = 1;\r
+\r
+ currentMoveString[0] = yytext[0];\r
+ currentMoveString[2] = yytext[1+skip];\r
+ currentMoveString[3] = yytext[2+skip];\r
+\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(currentMoveString[0] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[3] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[3] - ONE < 0 ||\r
+ currentMoveString[2] - AAA >= BOARD_RGHT ||\r
+ currentMoveString[0] - AAA < BOARD_LEFT ||\r
+ currentMoveString[2] - AAA < BOARD_LEFT )\r
+ return 0;\r
+\r
+ if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */\r
+ currentMoveString[0] != currentMoveString[2] ) {\r
+ currentMoveString[1] = yytext[2+skip];\r
+ } else \r
+ if (WhiteOnMove(yyboardindex)) {\r
+ if (yytext[2+skip] == ONE) return (int) ImpossibleMove;\r
+ currentMoveString[1] = yytext[2+skip] - 1;\r
+ if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn) \r
+ return ImpossibleMove;\r
+ } else {\r
+ currentMoveString[1] = currentMoveString[3] + 1;\r
+ if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove;\r
+ if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn) \r
+ return ImpossibleMove;\r
+ }\r
+ if (yyleng-skip > 3) {\r
+ if (yytext[yyleng-1] == ')')\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-2]);\r
+ else\r
+ c = currentMoveString[4] = ToLower(yytext[yyleng-1]);\r
+ currentMoveString[5] = NULLCHAR;\r
+ if(c != '=' && c != '+' && CharToPiece(c) == EmptySquare)\r
+ return IllegalMove;\r
+ } else {\r
+ currentMoveString[4] = NULLCHAR;\r
+ }\r
+\r
+ result = LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+ EP_UNKNOWN,\r
+ initialRights, /* [HGM] assume all castlings allowed */\r
+ currentMoveString[1] - ONE,\r
+ currentMoveString[0] - AAA,\r
+ currentMoveString[3] - ONE,\r
+ currentMoveString[2] - AAA,\r
+ currentMoveString[4]);\r
+\r
+ if (currentMoveString[4] == NULLCHAR &&\r
+ (result == WhitePromotionQueen || result == BlackPromotionQueen ||\r
+ result == WhitePromotionKnight || result == BlackPromotionKnight)) {\r
+ currentMoveString[4] = PieceToChar(BlackQueen);\r
+ // [HGM] shatranj: take care of variants without Queen\r
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier)\r
+ currentMoveString[4] = PieceToChar(BlackFerz);\r
+ if(gameInfo.variant == VariantGreat)\r
+ currentMoveString[4] = PieceToChar(BlackMan);\r
+ currentMoveString[5] = NULLCHAR;\r
+ }\r
+\r
+ if (result != IllegalMove) return (int) result;\r
+\r
+ /* Special case: improperly written en passant capture */\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ if (currentMoveString[3] == '5') {\r
+ currentMoveString[1] = '5';\r
+ currentMoveString[3] = '6';\r
+ } else {\r
+ return (int) IllegalMove;\r
+ }\r
+ } else {\r
+ if (currentMoveString[3] == '4') {\r
+ currentMoveString[1] = '4';\r
+ currentMoveString[3] = '3';\r
+ } else {\r
+ return (int) IllegalMove;\r
+ }\r
+ }\r
+\r
+ result = LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+ EP_UNKNOWN,\r
+ initialRights, /* [HGM] assume all castlings allowed */\r
+ currentMoveString[1] - ONE,\r
+ currentMoveString[0] - AAA,\r
+ currentMoveString[3] - ONE,\r
+ currentMoveString[2] - AAA,\r
+ currentMoveString[4]);\r
+\r
+ if (result == WhiteCapturesEnPassant || result == BlackCapturesEnPassant)\r
+ return (int) result;\r
+ else\r
+ return (int) IllegalMove;\r
+}\r
+\r
+"+"?[A-Z][xX:-]?[a-l][0-9]=? {\r
+ /*\r
+ * piece move, possibly ambiguous\r
+ */\r
+ DisambiguateClosure cl;\r
+ int skip = 0, skip2 = 0, promoted = 0;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ if(yytext[0] == '+') promoted = skip = skip2 = 1;\r
+\r
+ /* remove the [xX:-] */\r
+ if ((yytext[1+skip] == 'x') || (yytext[1+skip] == 'X')\r
+ || (yytext[1+skip] == ':') || (yytext[1+skip] == '-')) skip++;\r
+\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));\r
+ } else {\r
+ cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));\r
+ }\r
+ if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);\r
+\r
+ cl.rfIn = -1;\r
+ cl.ffIn = -1;\r
+ cl.rtIn = yytext[2+skip] - ONE;\r
+ cl.ftIn = yytext[1+skip] - AAA;\r
+ cl.promoCharIn = NULLCHAR;\r
+\r
+ if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */\r
+ cl.promoCharIn = yytext[yyleng-1];\r
+\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n",\r
+ yyleng,\r
+ cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' ');\r
+ }\r
+\r
+ /* [HGM] but do not allow values beyond board size */\r
+ if(cl.rtIn >= BOARD_HEIGHT ||\r
+ cl.rtIn < 0 ||\r
+ cl.ftIn >= BOARD_RGHT ||\r
+ cl.ftIn < BOARD_LEFT )\r
+ return 0;\r
+\r
+ Disambiguate(boards[yyboardindex],\r
+ PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+\r
+ currentMoveString[0] = cl.ff + AAA;\r
+ currentMoveString[1] = cl.rf + ONE;\r
+ currentMoveString[2] = cl.ft + AAA;\r
+ currentMoveString[3] = cl.rt + ONE;\r
+ currentMoveString[4] = cl.promoChar;\r
+ currentMoveString[5] = NULLCHAR;\r
+\r
+ return (int) cl.kind;\r
+}\r
+\r
+"+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=? {\r
+ /*\r
+ * piece move with rank or file disambiguator\r
+ */\r
+ DisambiguateClosure cl;\r
+ int skip = 0, skip2 = 0; int promoted=0;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ if(yytext[0]=='+') promoted = skip = skip2 = 1;\r
+\r
+ /* remove the [xX:-] */\r
+ if ((yytext[2+skip] == 'x') || (yytext[2+skip] == 'X')\r
+ || (yytext[2+skip] == ':') || (yytext[2+skip] == '-')) skip++;\r
+\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ cl.pieceIn = CharToPiece(ToUpper(yytext[skip2]));\r
+ } else {\r
+ cl.pieceIn = CharToPiece(ToLower(yytext[skip2]));\r
+ }\r
+ if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);\r
+\r
+ if (isalpha(yytext[1+skip2])) {\r
+ cl.rfIn = -1;\r
+ cl.ffIn = yytext[1+skip2] - AAA;\r
+ \r
+ if(cl.ffIn >= BOARD_RGHT ||\r
+ cl.ffIn < BOARD_LEFT ) return 0;\r
+ } else {\r
+ cl.rfIn = yytext[1+skip2] - ONE;\r
+ cl.ffIn = -1;\r
+ if(cl.rfIn >= BOARD_HEIGHT ||\r
+ cl.rfIn < 0) return 0;\r
+ }\r
+ cl.rtIn = yytext[3+skip] - ONE;\r
+ cl.ftIn = yytext[2+skip] - AAA;\r
+ cl.promoCharIn = NULLCHAR;\r
+\r
+ if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */\r
+ cl.promoCharIn = yytext[yyleng-1];\r
+\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(cl.rtIn >= BOARD_HEIGHT ||\r
+ cl.rtIn < 0 ||\r
+ cl.ftIn >= BOARD_RGHT ||\r
+ cl.ftIn < BOARD_LEFT )\r
+ return 0;\r
+\r
+ Disambiguate(boards[yyboardindex],\r
+ PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+\r
+ currentMoveString[0] = cl.ff + AAA;\r
+ currentMoveString[1] = cl.rf + ONE;\r
+ currentMoveString[2] = cl.ft + AAA;\r
+ currentMoveString[3] = cl.rt + ONE;\r
+ currentMoveString[4] = cl.promoChar;\r
+ currentMoveString[5] = NULLCHAR;\r
+\r
+ return (int) cl.kind;\r
+}\r
+\r
+000|0-0-0|ooo|OOO|o-o-o|O-O-O {\r
+ int rf, ff, rt, ft;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ /* [HGM] all squares referenced to board edges in stead of absolute */\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {\r
+ /* ICS wild castling */\r
+ rf = 0;\r
+ ff = (BOARD_WIDTH-1)>>1;\r
+ rt = 0;\r
+ ft = BOARD_RGHT-3;\r
+ } else {\r
+ rf = 0;\r
+ ff = BOARD_WIDTH>>1;\r
+ rt = 0;\r
+ ft = BOARD_LEFT+2;\r
+ }\r
+ } else{ \r
+ if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {\r
+ /* ICS wild castling */\r
+ rf = BOARD_HEIGHT-1;\r
+ ff = (BOARD_WIDTH-1)>>1;\r
+ rt = BOARD_HEIGHT-1;\r
+ ft = BOARD_RGHT-3;\r
+ } else {\r
+ rf = BOARD_HEIGHT-1;\r
+ ff = BOARD_WIDTH>>1;\r
+ rt = BOARD_HEIGHT-1;\r
+ ft = BOARD_LEFT+2;\r
+ }\r
+ }\r
+ if(gameInfo.variant == VariantFischeRandom) {\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ ff = initialRights[2];\r
+ ft = initialRights[1];\r
+ } else {\r
+ ff = initialRights[5];\r
+ ft = initialRights[4];\r
+ }\r
+ fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);\r
+ if(ff < 0 || ft < 0) return 0;\r
+ }\r
+ sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "long castling %d %d\n", ff, ft);\r
+ }\r
+ return (int) LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
+ EP_UNKNOWN,\r
+ castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
+ rf, ff, rt, ft, NULLCHAR);\r
+}\r
+\r
+00|0-0|oo|OO|o-o|O-O {\r
+ int rf, ff, rt, ft;\r
+\r
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
+\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ if (boards[yyboardindex][0][(BOARD_WIDTH-1)>>1] == WhiteKing) {\r
+ /* ICS wild castling */\r
+ rf = 0;\r
+ ff = (BOARD_WIDTH-1)>>1;\r
+ rt = 0;\r
+ ft = BOARD_LEFT+1;\r
+ } else {\r
+ rf = 0;\r
+ ff = BOARD_WIDTH>>1;\r
+ rt = 0;\r
+ ft = BOARD_RGHT-2;\r
+ }\r
+ } else {\r
+ if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {\r
+ /* ICS wild castling */\r
+ rf = BOARD_HEIGHT-1;\r
+ ff = (BOARD_WIDTH-1)>>1;\r
+ rt = BOARD_HEIGHT-1;\r
+ ft = BOARD_LEFT+1;\r
+ } else {\r
+ rf = BOARD_HEIGHT-1;\r
+ ff = BOARD_WIDTH>>1;\r
+ rt = BOARD_HEIGHT-1;\r
+ ft = BOARD_RGHT-2;\r
+ }\r
+ }\r
+ if(gameInfo.variant == VariantFischeRandom) {\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ ff = initialRights[2];\r
+ ft = initialRights[0];\r
+ } else {\r
+ ff = initialRights[5];\r
+ ft = initialRights[3];\r
+ }\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);\r
+ }\r
+ if(ff < 0 || ft < 0) return 0;\r
+ }\r
+ sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "short castling %d %d\n", ff, ft);\r
+ }\r
+\r
+ return (int) LegalityTest(boards[yyboardindex],\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
+ EP_UNKNOWN,\r
+ castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
+ rf, ff, rt, ft, NULLCHAR);\r
+}\r
+\r
+[A-Z][@*][a-l][0-9] {\r
+ /* Bughouse piece drop. No legality checking for now. */\r
+ currentMoveString[1] = '@';\r
+ currentMoveString[2] = yytext[2];\r
+ currentMoveString[3] = yytext[3];\r
+ currentMoveString[4] = NULLCHAR;\r
+\r
+ if (appData.debugMode) {\r
+ fprintf(debugFP, "Drop: %s\n", currentMoveString);\r
+ }\r
+ /* [HGM] do not allow values beyond board size */\r
+ if(currentMoveString[3] - ONE >= BOARD_HEIGHT ||\r
+ currentMoveString[2] - AAA >= BOARD_WIDTH )\r
+ return 0;\r
+\r
+ if (WhiteOnMove(yyboardindex)) {\r
+ currentMoveString[0] = ToUpper(yytext[0]);\r
+ return (int) WhiteDrop;\r
+ } else {\r
+ currentMoveString[0] = ToLower(yytext[0]);\r
+ return (int) BlackDrop;\r
+ }\r
+}\r
+\r
+[Rr]esign(s|ed)? {\r
+ if (WhiteOnMove(yyboardindex))\r
+ return (int) BlackWins;\r
+ else\r
+ return (int) WhiteWins;\r
+}\r
+\r
+(([Ww](hite)?)|([Bb](lack)?))" "(([Rr]esign)|([Ff]orfeit))(s|ed)? {\r
+ return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);\r
+}\r
+\r
+(([Ww](hite)?)|([Bb](lack)?))" "[Dd]isconnect(s|ed) {\r
+ return (int) GameUnfinished;\r
+}\r
+\r
+[Ss]talemate {\r
+ return (int) GameIsDrawn;\r
+}\r
+\r
+"+-+" {\r
+ return (int) GameIsDrawn;\r
+}\r
+\r
+([Cc]heck)?[Mm]ate {\r
+ if (WhiteOnMove(yyboardindex))\r
+ return (int) BlackWins;\r
+ else\r
+ return (int) WhiteWins;\r
+}\r
+\r
+"++" {\r
+ if (WhiteOnMove(yyboardindex))\r
+ return (int) BlackWins;\r
+ else\r
+ return (int) WhiteWins;\r
+}\r
+\r
+[Dd]raw(n)?(" "by)?(" "[Rr]epetition)|(" "[Aa]gree(d|ment)) {\r
+ return (int) GameIsDrawn;\r
+}\r
+\r
+[Dd]raw(n)?(" (".*")")? {\r
+ return (int) GameIsDrawn;\r
+}\r
+\r
+(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ww][io]n(s)?.*) {\r
+ return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins);\r
+}\r
+\r
+(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ll]os[tes]+.*) {\r
+ return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);\r
+}\r
+\r
+("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? { \r
+ return (int) WhiteWins;\r
+}\r
+\r
+("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? { \r
+ return (int) BlackWins;\r
+}\r
+\r
+("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {\r
+ return (int) GameIsDrawn;\r
+}\r
+\r
+("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {\r
+ return (int) GameUnfinished;\r
+}\r
+\r
+[1-9][0-9]*/"."?[ \t\n]*[a-lNnPpRrBQqKACFEWDGHOo] {\r
+ /* move numbers */\r
+ if ((yyleng == 1) && (yytext[0] == '1'))\r
+ return (int) MoveNumberOne;\r
+}\r
+\r
+\([0-9]+:[0-9][0-9](\.[0-9]+)?\)|\{[0-9]+:[0-9][0-9](\.[0-9]+)?\} {\r
+ /* elapsed time indication, e.g. (0:12) or {10:21.071} */ \r
+ return (int) ElapsedTime;\r
+}\r
+\r
+"[--"[^\]]*"--]" {\r
+ /* position diagram enclosed in [-- --] */\r
+ return (int) PositionDiagram;\r
+}\r
+\r
+^"{--------------"\n[^\}]*\n"--------------}"$ {\r
+ /* position diagram enclosed in {-- --} */\r
+ return (int) PositionDiagram;\r
+}\r
+\r
+\[[ \t\n]*[A-Za-z0-9][A-Za-z0-9_+#=-]*[ \t\n]*\"[^"]*\"[ \t\n]*\] {\r
+ return (int) PGNTag;\r
+} \r
+\r
+[Gg](nu|NU)" "?[Cc](hess|HESS).*[Gg](ame|AME) {\r
+ return (int) GNUChessGame;\r
+}\r
+\r
+^[#;%]" "[^ ]*(" game file"|" position file").*$ {\r
+ return (int) XBoardGame;\r
+}\r
+\r
+\$[0-9]+ { /* numeric annotation glyph */\r
+ return (int) NAG;\r
+}\r
+\r
+\{[^\}]*\} { /* anything in {} */\r
+ return (int) Comment; \r
+}\r
+\r
+;.*$ { /* ; to end of line */\r
+ return (int) Comment;\r
+}\r
+\r
+\[[^\]]*\] { /* anything in [] */\r
+ return (int) Comment; \r
+}\r
+\r
+\([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */\r
+ return (int) Comment; \r
+}\r
+\r
+\([^)][^)]+\) { /* >=2 chars in () */\r
+ return (int) Comment; \r
+} \r
+\r
+^[-a-zA-Z0-9]+:" ".*(\n[ \t]+.*)* {\r
+ /* Skip mail headers */\r
+}\r
+\r
+[a-zA-Z0-9'-]+ {\r
+ /* Skip random words */\r
+}\r
+\r
+.|\n {\r
+ /* Skip everything else */\r
+}\r
+\r
+%%\r
+\r
+\r
+static char *StringToLex;\r
+\r
+#ifndef FLEX_SCANNER\r
+static FILE *lexFP;\r
+\r
+static int input()\r
+{\r
+ int ret;\r
+ \r
+ if (StringToLex != NULL) {\r
+ ret = *StringToLex;\r
+ if (ret == NULLCHAR)\r
+ ret = EOF;\r
+ else\r
+ StringToLex++;\r
+ } else if (unputCount > 0) {\r
+ ret = unputBuffer[--unputCount];\r
+ } else {\r
+ ret = fgetc(lexFP);\r
+ } \r
+\r
+ if (ret == EOF) \r
+ return 0;\r
+ else\r
+ return ret;\r
+}\r
+\r
+/*\r
+ * Return offset of next pattern within current file\r
+ */\r
+int yyoffset()\r
+{\r
+ int offset = ftell(lexFP) - unputCount;\r
+\r
+ if (offset < 0) {\r
+ offset = 0;\r
+ }\r
+ return(offset);\r
+}\r
+ \r
+static void output(ch)\r
+ int ch;\r
+{\r
+ fprintf(stderr, "PARSER BUG: unmatched character '%c' (0%o)\n",\r
+ ch, ch);\r
+}\r
+\r
+static void unput(ch)\r
+ int ch;\r
+{\r
+ if (ch == 0) return;\r
+ if (StringToLex != NULL) {\r
+ StringToLex--;\r
+ } else {\r
+ if (unputCount >= UNPUT_BUF_SIZE)\r
+ fprintf(stderr, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",\r
+ ch, ch);\r
+ unputBuffer[unputCount++] = ch;\r
+ }\r
+}\r
+\r
+/* Get ready to lex from a new file. Kludge below sticks\r
+ an artificial newline at the front of the file, which the\r
+ above grammar ignores, but which makes ^ at start of pattern\r
+ match at the real start of the file.\r
+*/\r
+void yynewfile(f)\r
+ FILE *f;\r
+{\r
+ lexFP = f;\r
+ StringToLex = NULL;\r
+ unputCount = 0;\r
+ unput('\n'); /* kludge */\r
+}\r
+\r
+/* Get ready to lex from a string. ^ at start of pattern WON'T\r
+ match at the start of the string!\r
+*/\r
+void yynewstr(s)\r
+ char *s;\r
+{\r
+ lexFP = NULL;\r
+ StringToLex = s;\r
+ unputCount = 0;\r
+}\r
+#endif /*!FLEX_SCANNER*/\r
+\r
+#ifdef FLEX_SCANNER\r
+void my_yy_input(buf, result, max_size)\r
+ char *buf;\r
+ int *result;\r
+ int max_size;\r
+{\r
+ int count;\r
+\r
+ if (StringToLex != NULL) {\r
+ count = 0;\r
+ while (*StringToLex != NULLCHAR) {\r
+ *buf++ = *StringToLex++;\r
+ count++;\r
+ }\r
+ *result = count;\r
+ return;\r
+ } else {\r
+ count = fread(buf, 1, max_size, yyin);\r
+ if (count == 0) {\r
+ *result = YY_NULL;\r
+ } else {\r
+ *result = count;\r
+ }\r
+ return;\r
+ } \r
+}\r
+\r
+static YY_BUFFER_STATE my_file_buffer = NULL;\r
+\r
+/*\r
+ Return offset of next pattern in the current file.\r
+*/\r
+int yyoffset()\r
+{\r
+ int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;\r
+\r
+ return(ftell(YY_CURRENT_BUFFER->yy_input_file) -\r
+ yy_n_chars + pos);\r
+}\r
+\r
+\r
+void yynewstr(s)\r
+ char *s;\r
+{\r
+ if (my_file_buffer != NULL)\r
+ yy_delete_buffer(my_file_buffer);\r
+ StringToLex = s;\r
+ my_file_buffer = yy_create_buffer(stdin, YY_BUF_SIZE);\r
+ yy_switch_to_buffer(my_file_buffer);\r
+}\r
+\r
+void yynewfile(f)\r
+ FILE *f;\r
+{\r
+ if (my_file_buffer != NULL)\r
+ yy_delete_buffer(my_file_buffer);\r
+ StringToLex = NULL;\r
+ my_file_buffer = yy_create_buffer(f, YY_BUF_SIZE);\r
+ yy_switch_to_buffer(my_file_buffer);\r
+}\r
+#endif /*FLEX_SCANNER*/\r
+\r
+int yywrap()\r
+{\r
+ return TRUE;\r
+}\r
+\r
+/* Parse a move from the given string s */\r
+/* ^ at start of pattern WON'T work here unless using flex */\r
+ChessMove yylexstr(boardIndex, s)\r
+ int boardIndex;\r
+ char *s;\r
+{\r
+ ChessMove ret;\r
+ char *oldStringToLex;\r
+#ifdef FLEX_SCANNER\r
+ YY_BUFFER_STATE buffer, oldBuffer;\r
+#endif\r
+ \r
+ yyboardindex = boardIndex;\r
+ oldStringToLex = StringToLex;\r
+ StringToLex = s;\r
+#ifdef FLEX_SCANNER\r
+ buffer = yy_create_buffer(stdin, YY_BUF_SIZE);\r
+ oldBuffer = YY_CURRENT_BUFFER;\r
+ yy_switch_to_buffer(buffer);\r
+#endif /*FLEX_SCANNER*/\r
+\r
+ ret = (ChessMove) yylex();\r
+\r
+#ifdef FLEX_SCANNER\r
+ if (oldBuffer != NULL) \r
+ yy_switch_to_buffer(oldBuffer);\r
+ yy_delete_buffer(buffer);\r
+#endif /*FLEX_SCANNER*/\r
+ StringToLex = oldStringToLex;\r
+\r
+ return ret;\r
+}\r