%{\r
/*\r
* parser.l -- lex parser of algebraic chess moves for XBoard\r
- * $Id: parser.l,v 2.1 2003/10/27 19:21:00 mann Exp $\r
*\r
- * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.\r
- * Enhancements Copyright 1992-95 Free Software Foundation, Inc.\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
* SOFTWARE.\r
* ------------------------------------------------------------------------\r
*\r
- * The following terms apply to the enhanced version of XBoard distributed\r
- * by the Free Software Foundation:\r
+ * The following terms apply to the enhanced version of XBoard\r
+ * distributed by the Free Software Foundation:\r
* ------------------------------------------------------------------------\r
- * This program is free software; you can redistribute it and/or modify\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 2 of the License, or\r
- * (at your option) any later version.\r
+ * the Free Software Foundation, either version 3 of the License, or (at\r
+ * your option) any later version.\r
*\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\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, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
- * ------------------------------------------------------------------------\r
- */\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
currentMoveString[2] - AAA >= BOARD_RGHT ||\r
currentMoveString[0] - AAA < BOARD_LEFT ||\r
currentMoveString[2] - AAA < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\r
\r
piece = boards[yyboardindex]\r
[currentMoveString[1] - ONE][currentMoveString[0] - AAA];\r
return (int) IllegalMove;\r
\r
result = LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- initialRights, /* [HGM] assume all castlings allowed */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
currentMoveString[1] - ONE,\r
currentMoveString[0] - AAA,\r
currentMoveString[3] - ONE,\r
currentMoveString[2] - AAA >= BOARD_RGHT ||\r
currentMoveString[0] - AAA < BOARD_LEFT ||\r
currentMoveString[2] - AAA < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\r
\r
result = LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- initialRights, /* [HGM] assume all castlings allowed */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\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
+ if (currentMoveString[4] == NULLCHAR) {
+ if(result == WhitePromotionKnight || result == BlackPromotionKnight ||
+ result == WhitePromotionQueen || result == BlackPromotionQueen) {
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
+ currentMoveString[4] = PieceToChar(BlackFerz);
+ else if(gameInfo.variant == VariantGreat)
+ currentMoveString[4] = PieceToChar(BlackMan);
+ else
+ currentMoveString[4] = PieceToChar(BlackQueen);
+ currentMoveString[5] = NULLCHAR;
+ }
+ } else if(appData.testLegality && // strip off unnecessary and false promo characters
+ !(result == WhitePromotionQueen || result == BlackPromotionQueen ||
+ result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
+
+ return (int) result;
+}
+
+[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? {\r
/*\r
* Pawn move, possibly with promotion\r
*/\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
+ c = cl.promoCharIn = ToLower(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
+ return ImpossibleMove;\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
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
\r
currentMoveString[0] = cl.ff + AAA;\r
currentMoveString[1] = cl.rf + ONE;\r
cl.ffIn < BOARD_LEFT ||\r
cl.ftIn >= BOARD_RGHT ||\r
cl.ftIn < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\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
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
\r
currentMoveString[0] = cl.ff + AAA;\r
currentMoveString[1] = cl.rf + ONE;\r
currentMoveString[2] - AAA >= BOARD_RGHT ||\r
currentMoveString[0] - AAA < BOARD_LEFT ||\r
currentMoveString[2] - AAA < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\r
\r
if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */\r
currentMoveString[0] != currentMoveString[2] ) {\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
}\r
\r
result = LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- initialRights, /* [HGM] assume all castlings allowed */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
currentMoveString[1] - ONE,\r
currentMoveString[0] - AAA,\r
currentMoveString[3] - ONE,\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
+ if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)\r
currentMoveString[4] = PieceToChar(BlackFerz);\r
if(gameInfo.variant == VariantGreat)\r
currentMoveString[4] = PieceToChar(BlackMan);\r
}\r
\r
result = LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- initialRights, /* [HGM] assume all castlings allowed */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
currentMoveString[1] - ONE,\r
currentMoveString[0] - AAA,\r
currentMoveString[3] - ONE,\r
cl.rtIn < 0 ||\r
cl.ftIn >= BOARD_RGHT ||\r
cl.ftIn < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\r
\r
- Disambiguate(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
\r
currentMoveString[0] = cl.ff + AAA;\r
currentMoveString[1] = cl.rf + ONE;\r
cl.rtIn < 0 ||\r
cl.ftIn >= BOARD_RGHT ||\r
cl.ftIn < BOARD_LEFT )\r
- return 0;\r
+ return ImpossibleMove;\r
\r
- Disambiguate(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN, &cl);\r
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
\r
currentMoveString[0] = cl.ff + AAA;\r
currentMoveString[1] = cl.rf + ONE;\r
ft = BOARD_LEFT+2;\r
}\r
}\r
- if(gameInfo.variant == VariantFischeRandom) {\r
+ if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
if (WhiteOnMove(yyboardindex)) {\r
ff = initialRights[2];\r
ft = initialRights[1];\r
ff = initialRights[5];\r
ft = initialRights[4];\r
}\r
- fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);\r
+ if (appData.debugMode) \r
+ {\r
+ fprintf(debugFP, "Parser FRC long %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
fprintf(debugFP, "long castling %d %d\n", ff, ft);\r
}\r
return (int) LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
rf, ff, rt, ft, NULLCHAR);\r
}\r
\r
ft = BOARD_RGHT-2;\r
}\r
}\r
- if(gameInfo.variant == VariantFischeRandom) {\r
+ if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
if (WhiteOnMove(yyboardindex)) {\r
ff = initialRights[2];\r
ft = initialRights[0];\r
}\r
\r
return (int) LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex), EP_UNKNOWN,\r
- castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
+ PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
rf, ff, rt, ft, NULLCHAR);\r
}\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
+ return ImpossibleMove;\r
\r
if (WhiteOnMove(yyboardindex)) {\r
currentMoveString[0] = ToUpper(yytext[0]);\r
return (int) GameIsDrawn;\r
}\r
\r
-(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ww][io]n(s)?.*) {\r
+(([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([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
+(([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) { \r
return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);\r
}\r
\r
return (int) Comment; \r
}\r
\r
-\([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */\r
+\([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */\r
return (int) Comment; \r
}\r
\r
static void output(ch)\r
int ch;\r
{\r
- fprintf(stderr, "PARSER BUG: unmatched character '%c' (0%o)\n",\r
+ if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n",\r
ch, ch);\r
}\r
\r
StringToLex--;\r
} else {\r
if (unputCount >= UNPUT_BUF_SIZE)\r
- fprintf(stderr, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",\r
+ if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n",\r
ch, ch);\r
unputBuffer[unputCount++] = ch;\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 yylexstr(boardIndex, s, text, len)\r
+ int boardIndex, len;\r
+ char *s, *text;\r
+{
ChessMove ret;\r
char *oldStringToLex;\r
#ifdef FLEX_SCANNER\r
yy_switch_to_buffer(buffer);\r
#endif /*FLEX_SCANNER*/\r
\r
- ret = (ChessMove) yylex();\r
+ ret = (ChessMove) yylex();
+ strncpy(text, yy_text, len-1); // [HGM] vari: yy_text is not available to caller after buffer switch ?!?
+ text[len-1] = NULLCHAR;\r
\r
#ifdef FLEX_SCANNER\r
if (oldBuffer != NULL) \r