X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=parser.l;h=7dda43f4b8b41061adffee9fc74495772d2b7adc;hb=74113c74ca3c2294082fcf9dc87e2af8f991c3cc;hp=46de027b7bcce1e610285166735b774c6d06d145;hpb=93077be9f3009b8eb6ad64067f31d000aaf284c3;p=xboard.git diff --git a/parser.l b/parser.l index 46de027..7dda43f 100644 --- a/parser.l +++ b/parser.l @@ -7,10 +7,12 @@ %{ /* * parser.l -- lex parser of algebraic chess moves for XBoard - * $Id: parser.l,v 2.1 2003/10/27 19:21:00 mann Exp $ * - * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts. - * Enhancements Copyright 1992-95 Free Software Foundation, Inc. + * Copyright 1991 by Digital Equipment Corporation, Maynard, + * Massachusetts. + * + * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, + * 2006, 2007, 2008, 2009 Free Software Foundation, Inc. * * The following terms apply to Digital Equipment Corporation's copyright * interest in XBoard: @@ -34,24 +36,25 @@ * SOFTWARE. * ------------------------------------------------------------------------ * - * The following terms apply to the enhanced version of XBoard distributed - * by the Free Software Foundation: + * The following terms apply to the enhanced version of XBoard + * distributed by the Free Software Foundation: * ------------------------------------------------------------------------ - * This program is free software; you can redistribute it and/or modify + * + * 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 2 of the License, or - * (at your option) any later version. + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. * - * This program 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. + * 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * ------------------------------------------------------------------------ - */ + * along with this program. If not, see http://www.gnu.org/licenses/. + * + *------------------------------------------------------------------------ + ** See the file ChangeLog for a revision history. */ /* This parser handles all forms of promotion. * The parser resolves ambiguous moves by searching and check-testing. @@ -87,6 +90,16 @@ char *yy_text = (char *) yytext; #ifdef FLEX_SCANNER /* This is flex */ +/* [AP] use prototypes in function declarations */ +#define YY_USE_PROTOS + +#ifdef YY_USE_PROTOS +#define YY_PROTO(proto) proto +#else +#define YY_PROTO(proto) () +#endif +/* end of [AP] fix */ + #undef YY_INPUT #define YY_INPUT(buf, result, max_size) my_yy_input(buf, &result, max_size) #undef YY_DECL @@ -191,6 +204,11 @@ extern void CopyBoard P((Board to, Board from)); currentMoveString[3] = yytext[4+skip1+skip2]; currentMoveString[4] = NULLCHAR; + if (appData.debugMode) { + fprintf(debugFP, "Parser Qa1b2: yyleng=%d\n", + yyleng); + } + if (yyleng-skip1-skip2 > 5) { char c; if (yytext[yyleng-1] == ')') { c = currentMoveString[4] = ToLower(yytext[yyleng-2]); @@ -225,8 +243,7 @@ extern void CopyBoard P((Board to, Board from)); return (int) IllegalMove; result = LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -286,8 +303,7 @@ extern void CopyBoard P((Board to, Board from)); return 0; result = LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -297,7 +313,12 @@ extern void CopyBoard P((Board to, Board from)); if (currentMoveString[4] == NULLCHAR && (result == WhitePromotionKnight || result == BlackPromotionKnight || result == WhitePromotionQueen || result == BlackPromotionQueen)) { - currentMoveString[4] = PieceToChar(BlackQueen); + if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier) + currentMoveString[4] = PieceToChar(BlackFerz); + else if(gameInfo.variant == VariantGreat) + currentMoveString[4] = PieceToChar(BlackMan); + else + currentMoveString[4] = PieceToChar(BlackQueen); currentMoveString[5] = NULLCHAR; } @@ -335,8 +356,7 @@ extern void CopyBoard P((Board to, Board from)); return IllegalMove; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; @@ -390,8 +410,7 @@ extern void CopyBoard P((Board to, Board from)); if(c != '=' && c != '+' && c != NULLCHAR && CharToPiece(c) == EmptySquare) return IllegalMove; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; @@ -440,15 +459,18 @@ extern void CopyBoard P((Board to, Board from)); if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */ currentMoveString[0] != currentMoveString[2] ) { - if (yytext[2+skip] == ONE) return (int) ImpossibleMove; currentMoveString[1] = yytext[2+skip]; } else if (WhiteOnMove(yyboardindex)) { if (yytext[2+skip] == ONE) return (int) ImpossibleMove; currentMoveString[1] = yytext[2+skip] - 1; + if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != WhitePawn) + return ImpossibleMove; } else { currentMoveString[1] = currentMoveString[3] + 1; if (currentMoveString[3] == ONE+BOARD_HEIGHT-1) return (int) ImpossibleMove; + if(boards[yyboardindex][currentMoveString[1]-ONE][currentMoveString[0]-AAA] != BlackPawn) + return ImpossibleMove; } if (yyleng-skip > 3) { if (yytext[yyleng-1] == ')') @@ -463,8 +485,7 @@ extern void CopyBoard P((Board to, Board from)); } result = LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -475,6 +496,11 @@ extern void CopyBoard P((Board to, Board from)); (result == WhitePromotionQueen || result == BlackPromotionQueen || result == WhitePromotionKnight || result == BlackPromotionKnight)) { currentMoveString[4] = PieceToChar(BlackQueen); + // [HGM] shatranj: take care of variants without Queen + if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier) + currentMoveString[4] = PieceToChar(BlackFerz); + if(gameInfo.variant == VariantGreat) + currentMoveString[4] = PieceToChar(BlackMan); currentMoveString[5] = NULLCHAR; } @@ -498,8 +524,7 @@ extern void CopyBoard P((Board to, Board from)); } result = LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -543,6 +568,12 @@ extern void CopyBoard P((Board to, Board from)); if(yyleng-skip > 3) /* [HGM] can have Shogi-style promotion */ cl.promoCharIn = yytext[yyleng-1]; + if (appData.debugMode) { + fprintf(debugFP, "Parser Qa1: yyleng=%d, %d(%d,%d)-(%d,%d) = %d (%c)\n", + yyleng, + cl.pieceIn,cl.ffIn,cl.rfIn,cl.ftIn,cl.rtIn,cl.promoCharIn,cl.promoCharIn?cl.promoCharIn:' '); + } + /* [HGM] but do not allow values beyond board size */ if(cl.rtIn >= BOARD_HEIGHT || cl.rtIn < 0 || @@ -550,14 +581,13 @@ extern void CopyBoard P((Board to, Board from)); cl.ftIn < BOARD_LEFT ) return 0; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; currentMoveString[2] = cl.ft + AAA; currentMoveString[3] = cl.rt + ONE; - currentMoveString[4] = cl.promoChar; + currentMoveString[4] = cl.promoChar; currentMoveString[5] = NULLCHAR; return (int) cl.kind; @@ -611,8 +641,7 @@ extern void CopyBoard P((Board to, Board from)); cl.ftIn < BOARD_LEFT ) return 0; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; @@ -637,33 +666,46 @@ extern void CopyBoard P((Board to, Board from)); ff = (BOARD_WIDTH-1)>>1; rt = 0; ft = BOARD_RGHT-3; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } else { rf = 0; ff = BOARD_WIDTH>>1; rt = 0; ft = BOARD_LEFT+2; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } } else{ - if (boards[yyboardindex][BOARD_HEIGHT-1][3] == BlackKing) { + if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) { /* ICS wild castling */ rf = BOARD_HEIGHT-1; ff = (BOARD_WIDTH-1)>>1; rt = BOARD_HEIGHT-1; ft = BOARD_RGHT-3; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } else { rf = BOARD_HEIGHT-1; ff = BOARD_WIDTH>>1; rt = BOARD_HEIGHT-1; ft = BOARD_LEFT+2; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } } + if(gameInfo.variant == VariantFischeRandom) { + if (WhiteOnMove(yyboardindex)) { + ff = initialRights[2]; + ft = initialRights[1]; + } else { + ff = initialRights[5]; + ft = initialRights[4]; + } + if (appData.debugMode) + { + fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft); + }; + if(ff < 0 || ft < 0) return 0; + } + sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); + if (appData.debugMode) { + fprintf(debugFP, "long castling %d %d\n", ff, ft); + } return (int) LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.! rf, ff, rt, ft, NULLCHAR); } @@ -679,13 +721,11 @@ extern void CopyBoard P((Board to, Board from)); ff = (BOARD_WIDTH-1)>>1; rt = 0; ft = BOARD_LEFT+1; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } else { rf = 0; ff = BOARD_WIDTH>>1; rt = 0; ft = BOARD_RGHT-2; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } } else { if (boards[yyboardindex][BOARD_HEIGHT-1][(BOARD_WIDTH-1)>>1] == BlackKing) { @@ -694,18 +734,33 @@ extern void CopyBoard P((Board to, Board from)); ff = (BOARD_WIDTH-1)>>1; rt = BOARD_HEIGHT-1; ft = BOARD_LEFT+1; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } else { rf = BOARD_HEIGHT-1; ff = BOARD_WIDTH>>1; rt = BOARD_HEIGHT-1; ft = BOARD_RGHT-2; - sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); } } + if(gameInfo.variant == VariantFischeRandom) { + if (WhiteOnMove(yyboardindex)) { + ff = initialRights[2]; + ft = initialRights[0]; + } else { + ff = initialRights[5]; + ft = initialRights[3]; + } + if (appData.debugMode) { + fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft); + } + if(ff < 0 || ft < 0) return 0; + } + sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE); + if (appData.debugMode) { + fprintf(debugFP, "short castling %d %d\n", ff, ft); + } + return (int) LegalityTest(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ + PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.! rf, ff, rt, ft, NULLCHAR); } @@ -778,11 +833,11 @@ extern void CopyBoard P((Board to, Board from)); return (int) GameIsDrawn; } -(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ww][io]n(s)?.*) { +(([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ates)|([Ww][io]n(s)?)) { return (int) (ToUpper(yytext[0]) == 'W' ? WhiteWins : BlackWins); } -(([Ww](hite)?)|([Bb](lack)?))" "([Mm]ate(s|ed)?)|([Ll]os[tes]+.*) { +(([Ww](hite)?)|([Bb](lack)?))" "(([Mm]ated)|([Ll]os[tes]+)) { return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins); } @@ -851,7 +906,7 @@ extern void CopyBoard P((Board to, Board from)); return (int) Comment; } -\([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */ +\([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */ return (int) Comment; } @@ -917,7 +972,7 @@ int yyoffset() static void output(ch) int ch; { - fprintf(stderr, "PARSER BUG: unmatched character '%c' (0%o)\n", + if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unmatched character '%c' (0%o)\n", ch, ch); } @@ -929,7 +984,7 @@ static void unput(ch) StringToLex--; } else { if (unputCount >= UNPUT_BUF_SIZE) - fprintf(stderr, "PARSER BUG: unput buffer overflow '%c' (0%o)\n", + if(appData.debugMode) fprintf(debugFP, "PARSER BUG: unput buffer overflow '%c' (0%o)\n", ch, ch); unputBuffer[unputCount++] = ch; } @@ -995,9 +1050,9 @@ static YY_BUFFER_STATE my_file_buffer = NULL; */ int yyoffset() { - int pos = yy_c_buf_p - yy_current_buffer->yy_ch_buf; + int pos = yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf; - return(ftell(yy_current_buffer->yy_input_file) - + return(ftell(YY_CURRENT_BUFFER->yy_input_file) - yy_n_chars + pos); }