new developer release
[xboard.git] / parser.l
old mode 100755 (executable)
new mode 100644 (file)
index 77837a7..348bd15
--- a/parser.l
+++ b/parser.l
@@ -9,9 +9,10 @@
  * parser.l -- lex parser of algebraic chess moves for XBoard\r
  *\r
  * Copyright 1991 by Digital Equipment Corporation, Maynard,\r
- * Massachusetts.  Enhancements Copyright\r
- * 1992-2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software\r
- * Foundation, Inc.\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
@@ -243,8 +244,6 @@ extern void CopyBoard P((Board to, Board from));
 \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
@@ -305,8 +304,6 @@ extern void CopyBoard P((Board to, Board from));
 \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
@@ -316,7 +313,7 @@ extern void CopyBoard P((Board to, Board from));
     if (currentMoveString[4] == NULLCHAR &&\r
         (result == WhitePromotionKnight || result == BlackPromotionKnight ||\r
          result == WhitePromotionQueen  || result == BlackPromotionQueen)) {\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
         else if(gameInfo.variant == VariantGreat)\r
             currentMoveString[4] = PieceToChar(BlackMan);\r
@@ -359,8 +356,7 @@ extern void CopyBoard P((Board to, Board from));
       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
@@ -414,8 +410,7 @@ extern void CopyBoard P((Board to, Board from));
     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
@@ -491,8 +486,6 @@ extern void CopyBoard P((Board to, Board from));
 \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
@@ -504,7 +497,7 @@ extern void CopyBoard P((Board to, Board from));
          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
@@ -532,8 +525,6 @@ extern void CopyBoard P((Board to, Board from));
 \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
@@ -590,8 +581,7 @@ extern void CopyBoard P((Board to, Board from));
        cl.ftIn <  BOARD_LEFT     )\r
       return 0;\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
@@ -651,8 +641,7 @@ extern void CopyBoard P((Board to, Board from));
        cl.ftIn <  BOARD_LEFT     )\r
       return 0;\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
@@ -697,7 +686,7 @@ extern void CopyBoard P((Board to, Board from));
             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
@@ -705,7 +694,10 @@ extern void CopyBoard P((Board to, Board from));
             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
@@ -714,8 +706,6 @@ extern void CopyBoard P((Board to, Board from));
     }\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
@@ -751,7 +741,7 @@ extern void CopyBoard P((Board to, Board from));
             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
@@ -771,8 +761,6 @@ extern void CopyBoard P((Board to, Board from));
 \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
@@ -845,11 +833,11 @@ extern void CopyBoard P((Board to, Board from));
     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
@@ -918,7 +906,7 @@ extern void CopyBoard P((Board to, Board from));
     return (int) Comment; \r
 }\r
 \r
-\([^()]*(\([^()]*\)[^()]*)+[^()]*\)  {                 /* nested () */\r
+\([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\)  { /* very nested () */\r
     return (int) Comment; \r
 }\r
 \r
@@ -984,7 +972,7 @@ int yyoffset()
 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
@@ -996,7 +984,7 @@ static void unput(ch)
        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
@@ -1097,10 +1085,10 @@ int yywrap()
 \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
@@ -1116,7 +1104,9 @@ ChessMove yylexstr(boardIndex, s)
     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