Allow full promotion suffixes on SAN piece moves
[xboard.git] / parser.l
index accd570..ba9ffa6 100644 (file)
--- a/parser.l
+++ b/parser.l
@@ -236,6 +236,7 @@ extern void CopyBoard P((Board to, Board from));
 
     piece = boards[yyboardindex]
       [currentMoveString[1] - ONE][currentMoveString[0] - AAA];
+    if(PieceToChar(piece) == '+' && appData.icsActive) promoted = 1, yytext[skip3] = PieceToChar(DEMOTED piece); // trust ICS
     if(promoted) piece = (ChessSquare) (DEMOTED piece);
     c = PieceToChar(piece);
     if(c == '~') c = PieceToChar((ChessSquare) (DEMOTED piece));
@@ -251,9 +252,15 @@ extern void CopyBoard P((Board to, Board from));
                          currentMoveString[4]);
 
     if (currentMoveString[4] == NULLCHAR &&
-        (result == WhitePromotionKnight || result == BlackPromotionKnight ||
-         result == WhitePromotionQueen  || result == BlackPromotionQueen)) {
-        currentMoveString[4] = PieceToChar(BlackQueen);
+        (result == WhitePromotion  || result == BlackPromotion)) {
+        if(gameInfo.variant == VariantCourier || gameInfo.variant == VariantShatranj)
+            currentMoveString[4] = PieceToChar(BlackFerz);
+        else if(gameInfo.variant == VariantGreat)
+            currentMoveString[4] = PieceToChar(BlackMan);
+        else if(gameInfo.variant == VariantShogi)
+            currentMoveString[4] = '+';
+        else
+            currentMoveString[4] = PieceToChar(BlackQueen);
        currentMoveString[5] = NULLCHAR;
     }
 
@@ -311,19 +318,20 @@ extern void CopyBoard P((Board to, Board from));
                          currentMoveString[4]);
 
     if (currentMoveString[4] == NULLCHAR) {
-      if(result == WhitePromotionKnight || result == BlackPromotionKnight ||
-         result == WhitePromotionQueen  || result == BlackPromotionQueen) {
+      if(result == WhitePromotion  || result == BlackPromotion) {
         if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
             currentMoveString[4] = PieceToChar(BlackFerz);
         else if(gameInfo.variant == VariantGreat)
             currentMoveString[4] = PieceToChar(BlackMan);
+        else if(gameInfo.variant == VariantShogi)
+            currentMoveString[4] = '+'; // Queen might not be defined in mini variants!
         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;
+       !(result == WhitePromotion  || result == BlackPromotion ||
+         result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
 
     return (int) result;
 }
@@ -368,8 +376,7 @@ extern void CopyBoard P((Board to, Board from));
                          currentMoveString[4]);
 
     if (currentMoveString[4] == NULLCHAR &&
-        (result == WhitePromotionKnight || result == BlackPromotionKnight ||
-         result == WhitePromotionQueen  || result == BlackPromotionQueen)) {
+        (result == WhitePromotion  || result == BlackPromotion)) {
         if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
             currentMoveString[4] = PieceToChar(BlackFerz);
         else if(gameInfo.variant == VariantGreat)
@@ -377,9 +384,7 @@ extern void CopyBoard P((Board to, Board from));
         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;
 }
@@ -552,8 +557,7 @@ extern void CopyBoard P((Board to, Board from));
                          currentMoveString[4]);
 
     if (currentMoveString[4] == NULLCHAR &&
-        (result == WhitePromotionQueen  || result == BlackPromotionQueen ||
-         result == WhitePromotionKnight || result == BlackPromotionKnight)) {
+        (result == WhitePromotion  || result == BlackPromotion)) {
         currentMoveString[4] = PieceToChar(BlackQueen);
        // [HGM] shatranj: take care of variants without Queen
        if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
@@ -596,7 +600,7 @@ extern void CopyBoard P((Board to, Board from));
       return (int) IllegalMove;
 }
 
-"+"?[A-Z][xX:-]?[a-l][0-9]=?  {
+"+"?[A-Z][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)?  {
     /*
      * piece move, possibly ambiguous
      */
@@ -625,7 +629,7 @@ extern void CopyBoard P((Board to, Board from));
     cl.promoCharIn = NULLCHAR;
 
     if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */
-        cl.promoCharIn = yytext[yyleng-1];
+        cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
 
     if (appData.debugMode) {
         fprintf(debugFP, "Parser Qa1: yyleng=%d,  %d(%d,%d)-(%d,%d) = %d (%c)\n",
@@ -652,7 +656,7 @@ extern void CopyBoard P((Board to, Board from));
     return (int) cl.kind;
 }
 
-"+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]=?   {
+"+"?[A-Z][a-l0-9][xX:-]?[a-l][0-9]((=?\(?[A-Z]\)?)|=)?   {
     /*
      * piece move with rank or file disambiguator
      */
@@ -691,7 +695,7 @@ extern void CopyBoard P((Board to, Board from));
     cl.promoCharIn = NULLCHAR;
 
     if(yyleng-skip > 4) /* [HGM] can have Shogi-style promotion */
-        cl.promoCharIn = yytext[yyleng-1];
+        cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
 
     /* [HGM] do not allow values beyond board size */
     if(cl.rtIn >= BOARD_HEIGHT ||
@@ -746,6 +750,7 @@ extern void CopyBoard P((Board to, Board from));
        }
     }
     if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
+
         if (WhiteOnMove(yyboardindex)) {
             ff = initialRights[2];
             ft = initialRights[1];
@@ -823,7 +828,7 @@ extern void CopyBoard P((Board to, Board from));
                              rf, ff, rt, ft, NULLCHAR);
 }
 
-[A-Z][@*][a-l][0-9] {
+[A-Za-z][@*][a-l][0-9] {
     /* Bughouse piece drop. */
     currentMoveString[1] = '@';
     currentMoveString[2] = yytext[2];