X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=parser.l;h=53b568f29b2a8930df326612e3c62607345016b5;hb=e54045072f6e157b46a33104b8094cf9857f875d;hp=77837a7b6e2068eec9f8bd117872555d400a35ea;hpb=7b4dacf6fe9f8c10b6eb4d6070869a3d933dbeb5;p=xboard.git diff --git a/parser.l b/parser.l old mode 100755 new mode 100644 index 77837a7..53b568f --- a/parser.l +++ b/parser.l @@ -9,9 +9,10 @@ * parser.l -- lex parser of algebraic chess moves for XBoard * * Copyright 1991 by Digital Equipment Corporation, Maynard, - * Massachusetts. Enhancements Copyright - * 1992-2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software - * Foundation, Inc. + * 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: @@ -231,7 +232,7 @@ extern void CopyBoard P((Board to, Board from)); currentMoveString[2] - AAA >= BOARD_RGHT || currentMoveString[0] - AAA < BOARD_LEFT || currentMoveString[2] - AAA < BOARD_LEFT ) - return 0; + return ImpossibleMove; piece = boards[yyboardindex] [currentMoveString[1] - ONE][currentMoveString[0] - AAA]; @@ -243,8 +244,6 @@ extern void CopyBoard P((Board to, Board from)); result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! - EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -301,12 +300,10 @@ extern void CopyBoard P((Board to, Board from)); currentMoveString[2] - AAA >= BOARD_RGHT || currentMoveString[0] - AAA < BOARD_LEFT || currentMoveString[2] - AAA < BOARD_LEFT ) - return 0; + return ImpossibleMove; result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! - EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -316,7 +313,7 @@ extern void CopyBoard P((Board to, Board from)); if (currentMoveString[4] == NULLCHAR && (result == WhitePromotionKnight || result == BlackPromotionKnight || result == WhitePromotionQueen || result == BlackPromotionQueen)) { - if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier) + if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) currentMoveString[4] = PieceToChar(BlackFerz); else if(gameInfo.variant == VariantGreat) currentMoveString[4] = PieceToChar(BlackMan); @@ -328,7 +325,7 @@ extern void CopyBoard P((Board to, Board from)); return (int) result; } -[a-l][0-9]((=?\(?[A-Z]\)?)|=)? { +[a-l][0-9]((=?\(?[A-Za-z]\)?)|=)? { /* * Pawn move, possibly with promotion */ @@ -346,21 +343,20 @@ extern void CopyBoard P((Board to, Board from)); cl.ffIn = yytext[0] - AAA; cl.rtIn = yytext[1] - ONE; cl.ftIn = yytext[0] - AAA; - c = cl.promoCharIn = yytext[2+skip]; + c = cl.promoCharIn = ToLower(yytext[2+skip]); /* [HGM] do not allow values beyond board size */ if(cl.rtIn >= BOARD_HEIGHT || cl.rtIn < 0 || cl.ffIn >= BOARD_RGHT || cl.ftIn < BOARD_LEFT ) - return 0; + return ImpossibleMove; 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; @@ -409,13 +405,12 @@ extern void CopyBoard P((Board to, Board from)); cl.ffIn < BOARD_LEFT || cl.ftIn >= BOARD_RGHT || cl.ftIn < BOARD_LEFT ) - return 0; + return ImpossibleMove; 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; @@ -460,7 +455,7 @@ extern void CopyBoard P((Board to, Board from)); currentMoveString[2] - AAA >= BOARD_RGHT || currentMoveString[0] - AAA < BOARD_LEFT || currentMoveString[2] - AAA < BOARD_LEFT ) - return 0; + return ImpossibleMove; if (gameInfo.variant == VariantXiangqi && /* [HGM] In Xiangqi rank stays same */ currentMoveString[0] != currentMoveString[2] ) { @@ -491,8 +486,6 @@ extern void CopyBoard P((Board to, Board from)); result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! - EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -504,7 +497,7 @@ extern void CopyBoard P((Board to, Board from)); result == WhitePromotionKnight || result == BlackPromotionKnight)) { currentMoveString[4] = PieceToChar(BlackQueen); // [HGM] shatranj: take care of variants without Queen - if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier) + if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) currentMoveString[4] = PieceToChar(BlackFerz); if(gameInfo.variant == VariantGreat) currentMoveString[4] = PieceToChar(BlackMan); @@ -532,8 +525,6 @@ extern void CopyBoard P((Board to, Board from)); result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.! - EP_UNKNOWN, - initialRights, /* [HGM] assume all castlings allowed */ currentMoveString[1] - ONE, currentMoveString[0] - AAA, currentMoveString[3] - ONE, @@ -588,10 +579,9 @@ extern void CopyBoard P((Board to, Board from)); cl.rtIn < 0 || cl.ftIn >= BOARD_RGHT || cl.ftIn < BOARD_LEFT ) - return 0; + return ImpossibleMove; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; @@ -649,10 +639,9 @@ extern void CopyBoard P((Board to, Board from)); cl.rtIn < 0 || cl.ftIn >= BOARD_RGHT || cl.ftIn < BOARD_LEFT ) - return 0; + return ImpossibleMove; - Disambiguate(boards[yyboardindex], - PosFlags(yyboardindex), EP_UNKNOWN, &cl); + Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl); currentMoveString[0] = cl.ff + AAA; currentMoveString[1] = cl.rf + ONE; @@ -697,7 +686,7 @@ extern void CopyBoard P((Board to, Board from)); ft = BOARD_LEFT+2; } } - if(gameInfo.variant == VariantFischeRandom) { + if(PosFlags(0) & F_FRC_TYPE_CASTLING) { if (WhiteOnMove(yyboardindex)) { ff = initialRights[2]; ft = initialRights[1]; @@ -705,7 +694,10 @@ extern void CopyBoard P((Board to, Board from)); ff = initialRights[5]; ft = initialRights[4]; } - fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft); + 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); @@ -714,8 +706,6 @@ extern void CopyBoard P((Board to, Board from)); } return (int) LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.! - EP_UNKNOWN, - castlingRights[yyboardindex], /* [HGM] use true castling rights */ rf, ff, rt, ft, NULLCHAR); } @@ -751,7 +741,7 @@ extern void CopyBoard P((Board to, Board from)); ft = BOARD_RGHT-2; } } - if(gameInfo.variant == VariantFischeRandom) { + if(PosFlags(0) & F_FRC_TYPE_CASTLING) { if (WhiteOnMove(yyboardindex)) { ff = initialRights[2]; ft = initialRights[0]; @@ -771,8 +761,6 @@ extern void CopyBoard P((Board to, Board from)); return (int) LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.! - EP_UNKNOWN, - castlingRights[yyboardindex], /* [HGM] use true castling rights */ rf, ff, rt, ft, NULLCHAR); } @@ -789,7 +777,7 @@ extern void CopyBoard P((Board to, Board from)); /* [HGM] do not allow values beyond board size */ if(currentMoveString[3] - ONE >= BOARD_HEIGHT || currentMoveString[2] - AAA >= BOARD_WIDTH ) - return 0; + return ImpossibleMove; if (WhiteOnMove(yyboardindex)) { currentMoveString[0] = ToUpper(yytext[0]); @@ -845,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); } @@ -918,7 +906,7 @@ extern void CopyBoard P((Board to, Board from)); return (int) Comment; } -\([^()]*(\([^()]*\)[^()]*)+[^()]*\) { /* nested () */ +\([^()]*(\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)[^()]*)+[^()]*\) { /* very nested () */ return (int) Comment; } @@ -984,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); } @@ -996,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; } @@ -1097,10 +1085,10 @@ int yywrap() /* Parse a move from the given string s */ /* ^ at start of pattern WON'T work here unless using flex */ -ChessMove yylexstr(boardIndex, s) - int boardIndex; - char *s; -{ +ChessMove yylexstr(boardIndex, s, text, len) + int boardIndex, len; + char *s, *text; +{ ChessMove ret; char *oldStringToLex; #ifdef FLEX_SCANNER @@ -1116,7 +1104,9 @@ ChessMove yylexstr(boardIndex, s) yy_switch_to_buffer(buffer); #endif /*FLEX_SCANNER*/ - ret = (ChessMove) yylex(); + 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; #ifdef FLEX_SCANNER if (oldBuffer != NULL)