Updated copyright notice to 2012
[xboard.git] / parser.c
index 09bb390..a0a7ad1 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,13 +1,24 @@
-// New PGN parser by by HGM. I was dissatisfied with the old flex-generated parser for several reasons:
-// 1) It required flex to build
-// 2) It was not possible to use variant-dependent syntax, which gave trouble for '+' as Sogi promoChar vs check symbol
-// 3) It could not handle double-digit rank numbers
-// 4) It could not handle PSN moves, with (alpha rank and file digit)
-// 5) Having more than 12 ranks would require extension of the rules anyway
-// 6) It was cumbersome to maintain, which much code duplication that had to be kept in sync when changing something
-// 7) It needed special handling for packaging, because we wanted to include parser.c for people who had no flex
-// 8) It was quite large because of the table-driven flex algorithm.
-// This new parser suffers from none of that. It might even accomodate traditional Xiangqi notation at some future time.
+/*
+ * parser.c --
+ *
+ * Copyright 2011, 2012 Free Software Foundation, Inc.
+ * ------------------------------------------------------------------------
+ *
+ * GNU XBoard 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 3 of the License, or (at
+ * your option) any later version.
+ *
+ * GNU XBoard 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, see http://www.gnu.org/licenses/.  *
+ *
+ *------------------------------------------------------------------------
+ ** See the file ChangeLog for a revision history.  */
 
 #include "config.h"
 #include <stdio.h>
@@ -133,7 +144,7 @@ char PromoSuffix(char **p)
     char *start = *p;
     if(**p == 'e' && (Match("ep", p) || Match("e.p.", p))) { *p = start; return NULLCHAR; } // non-compliant e.p. suffix is no promoChar!
     if(**p == '+' && gameInfo.variant == VariantShogi) { (*p)++; return '+'; } 
-    if(**p == '=') (*p)++; //optional =
+    if(**p == '=' || (gameInfo.variant == VariantSChess) && **p == '/') (*p)++; // optional = (or / for Seirawan gating)
     if(**p == '(' && (*p)[2] == ')' && isalpha( (*p)[1] )) { (*p) += 3; return (*p)[-2]; }
     if(isalpha(**p)) return *(*p)++;
     if(*p != start) return '='; // must be the optional =
@@ -144,7 +155,7 @@ int NextUnit(char **p)
 {      // Main parser routine
        int coord[4], n, result, piece, i;
        char type[4], promoted, separator, slash, *oldp, *commentEnd, c;
-        int wom = WhiteOnMove(yyboardindex);
+        int wom = quickFlag ? quickFlag&1 : WhiteOnMove(yyboardindex);
 
        // ********* try white first, because it is so common **************************
        if(**p == ' ' || **p == '\n' || **p == '\t') { parseStart = (*p)++; return Nothing; }
@@ -323,7 +334,8 @@ badMove:// we failed to find algebraic move
                do (*p)++; while(isdigit(**p) || isalpha(**p) || **p == '+' ||
                                **p == '-' || **p == '=' || **p == '_' || **p == '#');
                SkipWhite(p);
-               if(*(*p)++ == '"') {
+               if(**p == '"') {
+                   (*p)++;
                    while(**p != '\n' && (*(*p)++ != '"'|| (*p)[-2] == '\\')); // look for unescaped quote
                    if((*p)[-1] !='"') { *p = oldp; Scan(']', p); return Comment; } // string closing delimiter missing
                    SkipWhite(p); if(*(*p)++ == ']') return PGNTag;
@@ -341,6 +353,9 @@ badMove:// we failed to find algebraic move
                    Match("OO", p) || Match("oo", p) || Match("00", p)) castlingType = 1;
            if(castlingType) { //code from old parser, collapsed for both castling types, and streamlined a bit
                int rf, ff, rt, ft; ChessSquare king;
+               char promo=NULLCHAR;
+
+               if(gameInfo.variant == VariantSChess) promo = PromoSuffix(p);
 
                if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
 
@@ -356,7 +371,7 @@ badMove:// we failed to find algebraic move
                ff = (BOARD_WIDTH-1)>>1; // this would be d-file
                if (boards[yyboardindex][rf][ff] == king) {
                    /* ICS wild castling */
-                   ft = castlingType == 1 ? BOARD_LEFT+1 : BOARD_RGHT-3;
+                   ft = castlingType == 1 ? BOARD_LEFT+1 : (gameInfo.variant == VariantJanus ? BOARD_RGHT-2 : BOARD_RGHT-3);
                } else {
                    ff = BOARD_WIDTH>>1; // e-file
                    ft = castlingType == 1 ? BOARD_RGHT-2 : BOARD_LEFT+2;
@@ -372,12 +387,12 @@ badMove:// we failed to find algebraic move
                    if (appData.debugMode) fprintf(debugFP, "Parser FRC (type=%d) %d %d\n", castlingType, ff, ft);
                    if(ff == NoRights || ft == NoRights) return ImpossibleMove;
                }
-               sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
+               sprintf(currentMoveString, "%c%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE,promo);
                if (appData.debugMode) fprintf(debugFP, "(%d-type) castling %d %d\n", castlingType, ff, ft);
 
                return (int) LegalityTest(boards[yyboardindex],
                              PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
-                             rf, ff, rt, ft, NULLCHAR);
+                             rf, ff, rt, ft, promo);
            }
        }