worked on premove bug
[xboard.git] / parser.l
old mode 100644 (file)
new mode 100755 (executable)
index da4cbc7..82957eb
--- a/parser.l
+++ b/parser.l
@@ -7,10 +7,12 @@
 %{\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
@@ -87,6 +90,16 @@ char *yy_text = (char *) yytext;
 \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
@@ -172,6 +185,7 @@ extern void CopyBoard P((Board to, Board from));
     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
@@ -190,6 +204,11 @@ extern void CopyBoard P((Board to, Board from));
     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
@@ -218,11 +237,14 @@ extern void CopyBoard P((Board to, Board from));
     piece = boards[yyboardindex]\r
       [currentMoveString[1] - ONE][currentMoveString[0] - AAA];\r
     if(promoted) piece = (ChessSquare) (DEMOTED piece);\r
-    if (ToLower(yytext[skip3]) != ToLower(PieceToChar(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), EP_UNKNOWN,\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
@@ -240,9 +262,10 @@ extern void CopyBoard P((Board to, Board from));
     return (int) result;\r
 }\r
 \r
-[a-l][0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)?      {\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
@@ -282,7 +305,8 @@ extern void CopyBoard P((Board to, Board from));
       return 0;\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\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
@@ -293,7 +317,12 @@ extern void CopyBoard P((Board to, Board from));
     if (currentMoveString[4] == NULLCHAR &&\r
         (result == WhitePromotionKnight || result == BlackPromotionKnight ||\r
          result == WhitePromotionQueen  || result == BlackPromotionQueen)) {\r
-        currentMoveString[4] = PieceToChar(BlackQueen);\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
@@ -436,15 +465,18 @@ extern void CopyBoard P((Board to, Board from));
 \r
     if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */\r
          currentMoveString[0] != currentMoveString[2] ) {\r
-        if (yytext[2+skip] == ONE) return (int) ImpossibleMove;\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
@@ -459,7 +491,8 @@ extern void CopyBoard P((Board to, Board from));
     }\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\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
@@ -471,6 +504,11 @@ extern void CopyBoard P((Board to, Board from));
         (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
@@ -494,7 +532,8 @@ extern void CopyBoard P((Board to, Board from));
     }\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\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
@@ -539,6 +578,12 @@ extern void CopyBoard P((Board to, Board from));
     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
@@ -553,7 +598,7 @@ extern void CopyBoard P((Board to, Board from));
     currentMoveString[1] = cl.rf + ONE;\r
     currentMoveString[2] = cl.ft + AAA;\r
     currentMoveString[3] = cl.rt + ONE;\r
-    currentMoveS\0tring[4] = cl.promoChar;\r
+    currentMoveString[4] = cl.promoChar;\r
     currentMoveString[5] = NULLCHAR;\r
 \r
     return (int) cl.kind;\r
@@ -633,33 +678,45 @@ extern void CopyBoard P((Board to, Board from));
             ff = (BOARD_WIDTH-1)>>1;\r
            rt = 0;\r
             ft = BOARD_RGHT-3;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        } else {\r
            rf = 0;\r
             ff = BOARD_WIDTH>>1;\r
            rt = 0;\r
             ft = BOARD_LEFT+2;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        }\r
     } else{ \r
-        if (boards[yyboardindex][BOARD_HEIGHT-1][3] == BlackKing) {\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
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        } else {\r
             rf = BOARD_HEIGHT-1;\r
             ff = BOARD_WIDTH>>1;\r
             rt = BOARD_HEIGHT-1;\r
             ft = BOARD_LEFT+2;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\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), EP_UNKNOWN,\r
-                              initialRights, /* [HGM] assume all castlings allowed */\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
@@ -675,13 +732,11 @@ extern void CopyBoard P((Board to, Board from));
             ff = (BOARD_WIDTH-1)>>1;\r
            rt = 0;\r
             ft = BOARD_LEFT+1;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        } else {\r
            rf = 0;\r
             ff = BOARD_WIDTH>>1;\r
            rt = 0;\r
             ft = BOARD_RGHT-2;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        }\r
     } else {\r
         if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) {\r
@@ -690,18 +745,35 @@ extern void CopyBoard P((Board to, Board from));
             ff = (BOARD_WIDTH-1)>>1;\r
             rt = BOARD_HEIGHT-1;\r
             ft = BOARD_LEFT+1;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
        } else {\r
             rf = BOARD_HEIGHT-1;\r
             ff = BOARD_WIDTH>>1;\r
             rt = BOARD_HEIGHT-1;\r
             ft = BOARD_RGHT-2;\r
-            sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\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), EP_UNKNOWN,\r
-                              initialRights, /* [HGM] assume all castlings allowed */\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
@@ -991,9 +1063,9 @@ static YY_BUFFER_STATE my_file_buffer = NULL;
 */\r
 int yyoffset()\r
 {\r
-    int pos = yy_c_buf_p - yy_current_buffer->yy_ch_buf;\r
+    int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf;\r
 \r
-    return(ftell(yy_current_buffer->yy_input_file) -\r
+    return(ftell(YY_CURRENT_BUFFER->yy_input_file) -\r
          yy_n_chars + pos);\r
 }\r
 \r