X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=parser.c;h=0938242c37449bb9bdd18a7f08336d7dc00643df;hb=ee5c4592139dec24b8514a6fd217600fa414a9a3;hp=4924bbd375436e5997c8072b46e78c032b787af0;hpb=0278ebc80411a20fd23303156285e55ce8d2b0a5;p=xboard.git diff --git a/parser.c b/parser.c index 4924bbd..0938242 100644 --- a/parser.c +++ b/parser.c @@ -22,6 +22,7 @@ #include "config.h" #include +#include #include #include #include "common.h" @@ -171,7 +172,7 @@ unsigned char kanjiTab[] = { int NextUnit P((char **p)); -int kifu = 0; +int kifu = 0, xqUBB = 0; char GetKanji (char **p, int start) @@ -284,7 +285,7 @@ KifuMove (char **p) int ReadLine () { // Read one line from the input file, and append to the buffer - char c, *start = inPtr; + int c; char *start = inPtr; if(fromString) return 0; // parsing string, so the end is a hard end if(!inputFile) return 0; while((c = fgetc(inputFile)) != EOF) { @@ -380,6 +381,7 @@ char PromoSuffix (char **p) { char *start = *p; + if(**p == ' ') return NULLCHAR; // common case, test explicitly for speed if(**p == 'e' && (Match("ep", p) || Match("e.p.", p))) { *p = start; return NULLCHAR; } // non-compliant e.p. suffix is no promoChar! if(**p == '+' && IS_SHOGI(gameInfo.variant)) { (*p)++; return '+'; } if(**p == '=' || (gameInfo.variant == VariantSChess) && **p == '/') (*p)++; // optional = (or / for Seirawan gating) @@ -402,7 +404,7 @@ NextUnit (char **p) if(**p == NULLCHAR) { // make sure there is something to parse if(fromString) return 0; // we are parsing string, so the end is really the end - *p = inPtr = inputBuf; + *p = inPtr = parseStart = inputBuf; if(!ReadLine()) return 0; // EOF } else if(inPtr > inputBuf + PARSEBUFSIZE/2) { // buffer fills up with already parsed stuff char *q = *p, *r = inputBuf; @@ -424,6 +426,14 @@ NextUnit (char **p) if(q = strchr(s, **p)) (*p)++, piece += 64*(q - s + 1); if(**p == '/') slash = *(*p)++; } + + if(xqUBB) { // Xiangqi UBB movelist + while(isdigit(**p)) { + type[n] = NUMERIC; coord[n++] = *(*p)++ - '0'; + if(n >= 4) break; + } + if(n < 4) *p -= n, xqUBB = n = 0; else type[0] = type[2] = ALPHABETIC, coord[1] = 9 - coord[1], coord[3] = 9 - coord[3]; + } while(n < 4) { if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC; else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC; @@ -494,7 +504,7 @@ NextUnit (char **p) if(piece) { cl.pieceIn = CharToPiece(wom ? piece : piece + 'a' - 'A'); if(cl.pieceIn == EmptySquare) return ImpossibleMove; // non-existent piece - if(promoted) cl.pieceIn = (ChessSquare) (CHUPROMOTED cl.pieceIn); + if(promoted) cl.pieceIn = (ChessSquare) (CHUPROMOTED(cl.pieceIn)); } else cl.pieceIn = EmptySquare; if(separator == '@' || separator == '*') { // drop move. We only get here without from-square or promoted piece fromY = DROP_RANK; fromX = cl.pieceIn; @@ -508,29 +518,42 @@ NextUnit (char **p) } if( type[1] != type[2] && // means fromY is of opposite type as ToX, or NOTHING (type[0] == NOTHING || type[0] == type[2]) ) { // well formed - + int suffix = 7; fromX = (currentMoveString[0] = coord[0] + 'a') - AAA; fromY = (currentMoveString[1] = coord[1] + '0') - ONE; currentMoveString[4] = cl.promoCharIn = PromoSuffix(p); currentMoveString[5] = NULLCHAR; + if(**p == 'x' && !cl.promoCharIn) { // other leg follows + char *q = *p; + int x = *++*p, y; + ++*p; y = Number(p); + if(**p == '-' || **p == 'x') { // 3-leg move! + currentMoveString[7] = (kill2X = toX) + AAA; // what we thought was to-square is in fact 1st kill-square of two + currentMoveString[8] = (kill2Y = toY) + ONE; // append it after 2nd kill-square + toX = x - AAA; // kludge alert: this will become 2nd kill square + toY = y + '0' - ONE; + suffix += 2; + } else *p = q; // 2-leg move, rewind to leave reading of 2nd leg to code below + } if(!cl.promoCharIn && (**p == '-' || **p == 'x')) { // Lion-type multi-leg move currentMoveString[5] = (killX = toX) + AAA; // what we thought was to-square is in fact kill-square currentMoveString[6] = (killY = toY) + ONE; // append it as suffix behind long algebraic move currentMoveString[4] = ';'; - currentMoveString[7] = NULLCHAR; + currentMoveString[suffix+1] = NULLCHAR; // read new to-square (VERY non-robust! Assumes correct (non-alpha-rank) syntax, and messes up on errors) toX = cl.ftIn = (currentMoveString[2] = *++*p) - AAA; ++*p; toY = cl.rtIn = (currentMoveString[3] = Number(p) + '0') - ONE; + currentMoveString[suffix] = cl.promoCharIn = PromoSuffix(p); } if(type[0] != NOTHING && type[1] != NOTHING && type[3] != NOTHING) { // fully specified. ChessSquare realPiece = boards[yyboardindex][fromY][fromX]; // Note that Disambiguate does not work for illegal moves, but flags them as impossible if(piece) { // check if correct piece indicated - if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED realPiece); + if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED(realPiece)); if(!(appData.icsActive && PieceToChar(realPiece) == '+') && // trust ICS if it moves promoted pieces piece && realPiece != cl.pieceIn) return ImpossibleMove; } else if(!separator && **p == '+') { // could be a protocol move, where bare '+' suffix means shogi-style promotion - if(realPiece < (wom ? WhiteCannon : BlackCannon) && PieceToChar(PROMOTED realPiece) == '+') // seems to be that + if(realPiece < (wom ? WhiteCannon : BlackCannon) && PieceToChar(PROMOTED(realPiece)) == '+') // seems to be that currentMoveString[4] = cl.promoCharIn = *(*p)++; // append promochar after all } result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), fromY, fromX, toY, toX, cl.promoCharIn); @@ -570,7 +593,7 @@ NextUnit (char **p) if(killX < 0) // [HGM] lion: do not overwrite kill-square suffix currentMoveString[4] = cl.promoChar; - if((cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) && (Match("ep", p) || Match("e.p.", p))); + if((cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) && !Match("ep", p)) Match("e.p.", p); return (int) cl.kind; } @@ -589,6 +612,13 @@ badMove:// we failed to find algebraic move *p = oldp; } SkipWhite(p); + if(Match("DhtmlXQ", p)) { // Xiangqi UBB tags + int res = Nothing; + if(**p == ']') strcpy(parseStart = yytext, "[Variant \"xiangqi\"]"), res = PGNTag; else + if(Match("_movelist", p)) xqUBB = 1; else Scan(']', p); + Scan(']', p); // for non-movelist tags this skips to the closing tag (disarming any enclosed kanji)! + return res; + } else if(Match("/DhtmlXQ", p)) { Scan(']', p); return Nothing; } if(isdigit(**p) || isalpha(**p)) { do (*p)++; while(isdigit(**p) || isalpha(**p) || **p == '+' || **p == '-' || **p == '=' || **p == '_' || **p == '#'); @@ -619,12 +649,12 @@ badMove:// we failed to find algebraic move if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */ if (wom) { - rf = 0; - rt = 0; + rf = castlingRank[0]; + rt = castlingRank[0]; king = WhiteKing; } else { - rf = BOARD_HEIGHT-1; - rt = BOARD_HEIGHT-1; + rf = castlingRank[3]; + rt = castlingRank[3]; king = BlackKing; } ff = (BOARD_WIDTH-1)>>1; // this would be d-file @@ -632,8 +662,12 @@ badMove:// we failed to find algebraic move /* ICS wild castling */ ft = castlingType == 1 ? BOARD_LEFT+1 : (gameInfo.variant == VariantJanus ? BOARD_RGHT-2 : BOARD_RGHT-3); } else { + char *q; ff = BOARD_WIDTH>>1; // e-file ft = castlingType == 1 ? BOARD_RGHT-2 : BOARD_LEFT+2; + if(pieceDesc[king] && (q = strchr(pieceDesc[king], 'O'))) { // redefined to non-default King stride + ft = (castlingType == 1 ? ff + atoi(q+1) : ff - atoi(q+1)); + } } if(PosFlags(0) & F_FRC_TYPE_CASTLING) { if (wom) {