- piece = 'P'; n = 4; // kludge alert: fake full to-square\r
- }\r
- }\r
- if(n == 4 && type[2] != type[3] && // we have a valid to-square (kludge: type[3] can be NOTHING on fxg type move)\r
- (piece || !promoted) && // promoted indicator only valid on named piece type\r
- (type[2] == ALPHABETIC || gameInfo.variant == VariantShogi)) { // in Shogi also allow alphabetic rank\r
- DisambiguateClosure cl;\r
- int fromX, fromY, toX, toY;\r
-\r
- if(slash && (!piece || type[1] == NOTHING)) goto badMove; // slash after piece only in ICS long format\r
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
-\r
- if(type[2] == NUMERIC) { // alpha-rank\r
- coord[2] = BOARD_RGHT - BOARD_LEFT - coord[2];\r
- coord[3] = BOARD_HEIGHT - coord[3];\r
- if(coord[0] >= 0) coord[0] = BOARD_RGHT - BOARD_LEFT - coord[0];\r
- if(coord[1] >= 0) coord[1] = BOARD_HEIGHT - coord[1];\r
- }\r
- toX = cl.ftIn = (currentMoveString[2] = coord[2] + 'a') - AAA;\r
- toY = cl.rtIn = (currentMoveString[3] = coord[3] + '0') - ONE;\r
- if(type[3] == NOTHING) cl.rtIn = -1; // for fxg type moves ask for toY disambiguation\r
- else if(toY >= BOARD_HEIGHT || toY < 0) return ImpossibleMove; // vert off-board to-square\r
- if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return ImpossibleMove;\r
- if(piece) {\r
- cl.pieceIn = CharToPiece(wom ? piece : ToLower(piece));\r
- if(cl.pieceIn == EmptySquare) return ImpossibleMove; // non-existent piece\r
- if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED cl.pieceIn);\r
- } else cl.pieceIn = EmptySquare;\r
- if(separator == '@' || separator == '*') { // drop move. We only get here without from-square or promoted piece\r
- fromY = DROP_RANK; fromX = cl.pieceIn;\r
- currentMoveString[0] = piece;\r
- currentMoveString[1] = '@';\r
- return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, fromY, fromX, toY, toX, NULLCHAR);\r
- }\r
- if(type[1] == NOTHING && type[0] != NOTHING) { // there is a disambiguator\r
- if(type[0] != type[2]) coord[0] = -1, type[1] = type[0], type[0] = NOTHING; // it was a rank-disambiguator\r
- }\r
- if( type[1] != type[2] && // means fromY is of opposite type as ToX, or NOTHING\r
- (type[0] == NOTHING || type[0] == type[2]) ) { // well formed\r
-\r
- fromX = (currentMoveString[0] = coord[0] + 'a') - AAA;\r
- fromY = (currentMoveString[1] = coord[1] + '0') - ONE;\r
- currentMoveString[4] = cl.promoCharIn = PromoSuffix(p);\r
- currentMoveString[5] = NULLCHAR;\r
- if(type[0] != NOTHING && type[1] != NOTHING && type[3] != NOTHING) { // fully specified.\r
- // Note that Disambiguate does not work for illegal moves, but flags them as impossible\r
- if(piece) { // check if correct piece indicated\r
- ChessSquare realPiece = boards[yyboardindex][fromY][fromX];\r
- if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED realPiece);\r
- if(!(appData.icsActive && PieceToChar(realPiece) == '+') && // trust ICS if it moves promoted pieces\r
- piece && realPiece != cl.pieceIn) return ImpossibleMove;\r
- }\r
- result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), fromY, fromX, toY, toX, cl.promoCharIn);\r
- if (currentMoveString[4] == NULLCHAR) { // suppy missing mandatory promotion character\r
- if(result == WhitePromotion || result == BlackPromotion) {\r
- switch(gameInfo.variant) {\r
- case VariantCourier:\r
- case VariantShatranj: currentMoveString[4] = PieceToChar(BlackFerz); break;\r
- case VariantGreat: currentMoveString[4] = PieceToChar(BlackMan); break;\r
- case VariantShogi: currentMoveString[4] = '+'; break;\r
- default: currentMoveString[4] = PieceToChar(BlackQueen);\r
- }\r
- } else if(result == WhiteNonPromotion || result == BlackNonPromotion) {\r
- currentMoveString[4] = '=';\r
- }\r
- } else if(appData.testLegality && gameInfo.variant != VariantSChess && // strip off unnecessary and false promo characters\r
- !(result == WhitePromotion || result == BlackPromotion ||\r
- result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;\r
- return result;\r
- } else if(cl.pieceIn == EmptySquare) cl.pieceIn = wom ? WhitePawn : BlackPawn;\r
- cl.ffIn = type[0] == NOTHING ? -1 : coord[0] + 'a' - AAA;\r
- cl.rfIn = type[1] == NOTHING ? -1 : coord[1] + '0' - ONE;\r
-\r
- Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
-\r
- if(cl.kind == ImpossibleMove && !piece && type[1] == NOTHING // fxg5 type\r
- && toY == (wom ? 4 : 3)) { // could be improperly written e.p.\r
- cl.rtIn += wom ? 1 : -1; // shift target square to e.p. square\r
- Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);\r
- if((cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant))\r
- return ImpossibleMove; // nice try, but no cigar\r
- }\r
-\r
- currentMoveString[0] = cl.ff + AAA;\r
- currentMoveString[1] = cl.rf + ONE;\r
- currentMoveString[3] = cl.rt + ONE;\r
- currentMoveString[4] = cl.promoChar;\r
-\r
- if((cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) && (Match("ep", p) || Match("e.p.", p)));\r
-\r
- return (int) cl.kind;\r
- }\r
- }\r
-badMove:// we failed to find algebraic move\r
-\r
-\r
- // ********* SAN Castings *************************************\r
- *p = oldp;\r
- if(**p == 'O' || **p == 'o' || **p == '0') {\r
- int castlingType = 0;\r
- if(Match("O-O-O", p) || Match("o-o-o", p) || Match("0-0-0", p) || \r
- Match("OOO", p) || Match("ooo", p) || Match("000", p)) castlingType = 2;\r
- else if(Match("O-O", p) || Match("o-o", p) || Match("0-0", p) ||\r
- Match("OO", p) || Match("oo", p) || Match("00", p)) castlingType = 1;\r
- if(castlingType) { //code from old parser, collapsed for both castling types, and streamlined a bit\r
- int rf, ff, rt, ft; ChessSquare king;\r
-\r
- if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */\r
-\r
- if (wom) {\r
- rf = 0;\r
- rt = 0;\r
- king = WhiteKing;\r
- } else {\r
- rf = BOARD_HEIGHT-1;\r
- rt = BOARD_HEIGHT-1;\r
- king = BlackKing;\r
- }\r
- ff = (BOARD_WIDTH-1)>>1; // this would be d-file\r
- if (boards[yyboardindex][rf][ff] == king) {\r
- /* ICS wild castling */\r
- ft = castlingType == 1 ? BOARD_LEFT+1 : BOARD_RGHT-3;\r
- } else {\r
- ff = BOARD_WIDTH>>1; // e-file\r
- ft = castlingType == 1 ? BOARD_RGHT-2 : BOARD_LEFT+2;\r
- }\r
- if(PosFlags(0) & F_FRC_TYPE_CASTLING) {\r
- if (wom) {\r
- ff = initialRights[2];\r
- ft = initialRights[castlingType-1];\r
- } else {\r
- ff = initialRights[5];\r
- ft = initialRights[castlingType+2];\r
- }\r
- if (appData.debugMode) fprintf(debugFP, "Parser FRC (type=%d) %d %d\n", castlingType, ff, ft);\r
- if(ff == NoRights || ft == NoRights) return ImpossibleMove;\r
- }\r
- sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);\r
- if (appData.debugMode) fprintf(debugFP, "(%d-type) castling %d %d\n", castlingType, ff, ft);\r
-\r
- return (int) LegalityTest(boards[yyboardindex],\r
- PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
- rf, ff, rt, ft, NULLCHAR);\r
- }\r
- }\r
-\r
-\r
- // ********* Comments and result messages **********************\r
- *p = oldp; commentEnd = NULL; result = 0;\r
- if(**p == '{') {\r
- if(RdTime('}', p)) return ElapsedTime;\r
- if(lastChar == '\n' && Match("--------------\n", p)) {\r
- char *q;\r
- i = Scan ('}', p); q = *p - 16;\r
- if(Match("\n--------------}\n", &q)) return PositionDiagram;\r
- } else i = Scan('}', p);\r
- commentEnd = *p; if(i) return Comment; // return comment that runs to EOF immediately\r
- }\r
- if(commentEnd) SkipWhite(p);\r
- if(Match("*", p)) result = GameUnfinished;\r
- else if(**p == '0') {\r
- if( Match("0-1", p) || Match("0/1", p) || Match("0:1", p) ||\r
- Match("0 - 1", p) || Match("0 / 1", p) || Match("0 : 1", p)) result = BlackWins;\r
- } else if(**p == '1') {\r
- if( Match("1-0", p) || Match("1/0", p) || Match("1:0", p) ||\r
- Match("1 - 0", p) || Match("1 / 0", p) || Match("1 : 0", p)) result = WhiteWins;\r
- else if(Match("1/2 - 1/2", p) || Match("1/2:1/2", p) || Match("1/2 : 1/2", p) || Match("1 / 2 - 1 / 2", p) ||\r
- Match("1 / 2 : 1 / 2", p) || Match("1/2", p) || Match("1 / 2", p)) result = GameIsDrawn;\r
- }\r
- if(result) {\r
- if(Match(" (", p) && !Scan(')', p) || Match(" {", p) && !Scan('}', p)) { // there is a comment after the PGN result!\r
- if(commentEnd) { *p = commentEnd; return Comment; } // so comment before it is normal comment; return that first\r
- }\r
- return result; // this returns a possible preceeding comment as result details\r
- }\r
- if(commentEnd) { *p = commentEnd; return Comment; } // there was no PGN result following, so return as normal comment\r
-\r
-\r
- // ********* Move numbers (after castlings or PGN results!) ***********\r
- if((i = Number(p)) != BADNUMBER) { // a single number was read as part of our attempt to read a move\r
- char *numEnd = *p;\r
- if(**p == '.') (*p)++; SkipWhite(p);\r
- if(**p == '+' || isalpha(**p) || gameInfo.variant == VariantShogi && *p != numEnd && isdigit(**p)) {\r
- *p = numEnd;\r
- return i == 1 ? MoveNumberOne : Nothing;\r
- }\r
- *p = numEnd; return Nothing;\r
- }\r
-\r
-\r
- // ********* non-compliant game-result indicators *********************\r
- if(Match("+-+", p) || Word("stalemate", p)) return GameIsDrawn;\r
- if(Match("++", p) || Verb("resign", p) || (Word("check", p) || 1) && Word("mate", p) )\r
- return (wom ? BlackWins : WhiteWins);\r
- c = ToUpper(**p);\r
- if(Word("w", p) && (Match("hite", p) || 1) || Word("b", p) && (Match("lack", p) || 1) ) {\r
- if(**p != ' ') return Nothing;\r
- ++*p;\r
- if(Verb("disconnect", p)) return GameUnfinished;\r
- if(Verb("resign", p) || Verb("forfeit", p) || Word("mated", p) || Word("lost", p) || Word("loses", p))\r
- return (c == 'W' ? BlackWins : WhiteWins);\r
- if(Word("mates", p) || Word("wins", p) || Word("won", p))\r
- return (c != 'W' ? BlackWins : WhiteWins);\r
- return Nothing;\r
- }\r
- if(Word("draw", p)) {\r
- if(**p == 'n') (*p)++;\r
- if(**p != ' ') return GameIsDrawn;\r
- oldp = ++*p;\r
- if(Word("agreed", p)) return GameIsDrawn;\r
- if(Match("by ", p) && (Word("repetition", p) || Word("agreement", p)) ) return GameIsDrawn;\r
- *p = oldp;\r
- if(*(*p)++ == '(') {\r
- while(**p != '\n') if(*(*p)++ == ')') break;\r
- if((*p)[-1] == ')') return GameIsDrawn;\r
- }\r
- *p = oldp - 1; return GameIsDrawn;\r
- }\r
-\r
-\r
- // ********* Numeric annotation glyph **********************************\r
- if(**p == '$') { (*p)++; if(Number(p) != BADNUMBER) return NAG; return Nothing; }\r
-\r
-\r
- // ********** by now we are getting down to the silly stuff ************\r
- if(Word("gnu", p) || Match("GNU", p)) {\r
- if(**p == ' ') (*p)++;\r
- if(Word("chess", p) || Match("CHESS", p)) {\r
- char *q;\r
- if((q = strstr(*p, "game")) || (q = strstr(*p, "GAME")) || (q = strstr(*p, "Game"))) {\r
- (*p) = q + 4; return GNUChessGame;\r
- }\r
- }\r
- return Nothing;\r
- }\r
- if(lastChar == '\n' && (Match("# ", p) || Match("; ", p) || Match("% ", p))) {\r
- while(**p != '\n' && **p != ' ') (*p)++;\r
- if(**p == ' ' && (Match(" game file", p) || Match(" position file", p))) {\r
- while(**p != '\n') (*p)++; // skip to EOLN\r
- return XBoardGame;\r
- }\r
- *p = oldp; // we might need to re-match the skipped stuff\r
- }\r
-\r
-\r
- // ********* Efficient skipping of (mostly) alphabetic chatter **********\r
- while(isdigit(**p) || isalpha(**p) || **p == '-') (*p)++;\r
- if(*p != oldp) {\r
- if(**p == '\'') {\r
- while(isdigit(**p) || isalpha(**p) || **p == '-' || **p == '\'') (*p)++;\r
- return Nothing; // random word\r
- }\r
- if(lastChar == '\n' && Match(": ", p)) { // mail header, skip indented lines\r
- do {\r
- while(**p != '\n') (*p)++;\r
- if(!ReadLine()) return Nothing; // append next line if not EOF\r
- } while(Match("\n ", p) || Match("\n\t", p));\r
- }\r
- return Nothing;\r
- }\r
-\r
-\r
- // ********* Could not match to anything. Return offending character ****\r
- (*p)++;\r
- return Nothing;\r
-}\r
-\r
-/*\r
- Return offset of next pattern in the current file.\r
-*/\r
-int yyoffset()\r
-{\r
- return ftell(inputFile) - (inPtr - parsePtr); // subtract what is read but not yet parsed\r
-}\r
-\r
-void yynewfile (FILE *f)\r
-{ // prepare parse buffer for reading file\r
- inputFile = f;\r
- inPtr = parsePtr = inputBuf;\r
- fromString = 0;\r
- lastChar = '\n';\r
- *inPtr = NULLCHAR; // make sure we will start by reading a line\r
-}\r
-\r
-void yynewstr P((char *s))\r
-{\r
- parsePtr = s;\r
- inputFile = NULL;\r
- fromString = 1;\r
-}\r
-\r
-int yylex()\r
-{ // this replaces the flex-generated parser\r
- int result = NextUnit(&parsePtr);\r
- char *p = parseStart, *q = yytext;\r
- while(p < parsePtr) *q++ = *p++; // copy the matched text to yytext[]\r
- *q = NULLCHAR;\r
- lastChar = q[-1];\r
- return result;\r
-}\r
-\r
-int Myylex()\r
-{ // [HGM] wrapper for yylex, which treats nesting of parentheses\r
- int symbol, nestingLevel = 0, i=0;\r
- char *p;\r
- static char buf[256*MSG_SIZ];\r
- buf[0] = NULLCHAR;\r
- do { // eat away anything not at level 0\r
- symbol = yylex();\r
- if(symbol == Open) nestingLevel++;\r
- if(nestingLevel) { // save all parsed text between (and including) the ()\r
- for(p=yytext; *p && i<256*MSG_SIZ-2;) buf[i++] = *p++;\r
- buf[i] = NULLCHAR;\r
- }\r
- if(symbol == 0) break; // ran into EOF\r
- if(symbol == Close) symbol = Comment, nestingLevel--;\r
- } while(nestingLevel || symbol == Nothing);\r
- yy_text = buf[0] ? buf : (char*)yytext;\r
- return symbol;\r
-}\r
-\r
-ChessMove yylexstr(int boardIndex, char *s, char *buf, int buflen)\r
-{\r
- ChessMove ret;\r
- char *savPP = parsePtr;\r
- fromString = 1;\r
- yyboardindex = boardIndex;\r
- parsePtr = s;\r
- ret = (ChessMove) Myylex();\r
- strncpy(buf, yy_text, buflen-1);\r
- buf[buflen-1] = NULLCHAR;\r
- parsePtr = savPP;\r
- fromString = 0;\r
- return ret;\r
-}\r
+ piece = 'P'; n = 4; // kludge alert: fake full to-square
+ }
+ } else if(n == 1 && type[0] == NUMERIC && coord[0] > 1) { while(**p == '.') (*p)++; return Nothing; } // fast exit for move numbers
+ if(n == 4 && type[2] != type[3] && // we have a valid to-square (kludge: type[3] can be NOTHING on fxg type move)
+ (piece || !promoted) && // promoted indicator only valid on named piece type
+ (type[2] == ALPHABETIC || gameInfo.variant == VariantShogi)) { // in Shogi also allow alphabetic rank
+ DisambiguateClosure cl;
+ int fromX, fromY, toX, toY;
+
+ if(slash && (!piece || type[1] == NOTHING)) goto badMove; // slash after piece only in ICS long format
+ if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
+
+ if(type[2] == NUMERIC) { // alpha-rank
+ coord[2] = BOARD_RGHT - BOARD_LEFT - coord[2];
+ coord[3] = BOARD_HEIGHT - coord[3];
+ if(coord[0] >= 0) coord[0] = BOARD_RGHT - BOARD_LEFT - coord[0];
+ if(coord[1] >= 0) coord[1] = BOARD_HEIGHT - coord[1];
+ }
+ toX = cl.ftIn = (currentMoveString[2] = coord[2] + 'a') - AAA;
+ toY = cl.rtIn = (currentMoveString[3] = coord[3] + '0') - ONE;
+ if(type[3] == NOTHING) cl.rtIn = -1; // for fxg type moves ask for toY disambiguation
+ else if(toY >= BOARD_HEIGHT || toY < 0) return ImpossibleMove; // vert off-board to-square
+ if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return ImpossibleMove;
+ if(piece) {
+ cl.pieceIn = CharToPiece(wom ? piece : ToLower(piece));
+ if(cl.pieceIn == EmptySquare) return ImpossibleMove; // non-existent piece
+ if(promoted) cl.pieceIn = (ChessSquare) (PROMOTED 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;
+ currentMoveString[0] = piece;
+ currentMoveString[1] = '@';
+ return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, fromY, fromX, toY, toX, NULLCHAR);
+ }
+ if(type[1] == NOTHING && type[0] != NOTHING) { // there is a disambiguator
+ if(type[0] != type[2]) coord[0] = -1, type[1] = type[0], type[0] = NOTHING; // it was a rank-disambiguator
+ }
+ if( type[1] != type[2] && // means fromY is of opposite type as ToX, or NOTHING
+ (type[0] == NOTHING || type[0] == type[2]) ) { // well formed
+
+ fromX = (currentMoveString[0] = coord[0] + 'a') - AAA;
+ fromY = (currentMoveString[1] = coord[1] + '0') - ONE;
+ currentMoveString[4] = cl.promoCharIn = PromoSuffix(p);
+ currentMoveString[5] = NULLCHAR;
+ if(type[0] != NOTHING && type[1] != NOTHING && type[3] != NOTHING) { // fully specified.
+ // Note that Disambiguate does not work for illegal moves, but flags them as impossible
+ if(piece) { // check if correct piece indicated
+ ChessSquare realPiece = boards[yyboardindex][fromY][fromX];
+ 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;
+ }
+ result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), fromY, fromX, toY, toX, cl.promoCharIn);
+ if (currentMoveString[4] == NULLCHAR) { // suppy missing mandatory promotion character
+ if(result == WhitePromotion || result == BlackPromotion) {
+ switch(gameInfo.variant) {
+ case VariantCourier:
+ case VariantShatranj: currentMoveString[4] = PieceToChar(BlackFerz); break;
+ case VariantGreat: currentMoveString[4] = PieceToChar(BlackMan); break;
+ case VariantShogi: currentMoveString[4] = '+'; break;
+ default: currentMoveString[4] = PieceToChar(BlackQueen);
+ }
+ } else if(result == WhiteNonPromotion || result == BlackNonPromotion) {
+ currentMoveString[4] = '=';
+ }
+ } else if(appData.testLegality && gameInfo.variant != VariantSChess && // strip off unnecessary and false promo characters
+ !(result == WhitePromotion || result == BlackPromotion ||
+ result == WhiteNonPromotion || result == BlackNonPromotion)) currentMoveString[4] = NULLCHAR;
+ return result;
+ } else if(cl.pieceIn == EmptySquare) cl.pieceIn = wom ? WhitePawn : BlackPawn;
+ cl.ffIn = type[0] == NOTHING ? -1 : coord[0] + 'a' - AAA;
+ cl.rfIn = type[1] == NOTHING ? -1 : coord[1] + '0' - ONE;
+
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
+
+ if(cl.kind == ImpossibleMove && !piece && type[1] == NOTHING // fxg5 type
+ && toY == (wom ? 4 : 3)) { // could be improperly written e.p.
+ cl.rtIn += wom ? 1 : -1; // shift target square to e.p. square
+ Disambiguate(boards[yyboardindex], PosFlags(yyboardindex), &cl);
+ if((cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant))
+ return ImpossibleMove; // nice try, but no cigar
+ }
+
+ currentMoveString[0] = cl.ff + AAA;
+ currentMoveString[1] = cl.rf + ONE;
+ currentMoveString[3] = cl.rt + ONE;
+ currentMoveString[4] = cl.promoChar;
+
+ if((cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) && (Match("ep", p) || Match("e.p.", p)));
+
+ return (int) cl.kind;
+ }
+ }
+badMove:// we failed to find algebraic move
+ *p = oldp;
+
+
+ // Next we do some common symbols where the first character commits us to things that cannot possibly be a move
+
+ // ********* PGN tags ******************************************
+ if(**p == '[') {
+ oldp = ++(*p);
+ if(Match("--", p)) { // "[--" could be start of position diagram
+ if(!Scan(']', p) && (*p)[-3] == '-' && (*p)[-2] == '-') return PositionDiagram;
+ *p = oldp;
+ }
+ SkipWhite(p);
+ if(isdigit(**p) || isalpha(**p)) {
+ do (*p)++; while(isdigit(**p) || isalpha(**p) || **p == '+' ||
+ **p == '-' || **p == '=' || **p == '_' || **p == '#');
+ SkipWhite(p);
+ if(*(*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;
+ }
+ }
+ Scan(']', p); return Comment;
+ }
+
+ // ********* SAN Castings *************************************
+ if(**p == 'O' || **p == 'o' || **p == '0') {
+ int castlingType = 0;
+ if(Match("O-O-O", p) || Match("o-o-o", p) || Match("0-0-0", p) ||
+ Match("OOO", p) || Match("ooo", p) || Match("000", p)) castlingType = 2;
+ else if(Match("O-O", p) || Match("o-o", p) || Match("0-0", p) ||
+ 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 */
+
+ if (wom) {
+ rf = 0;
+ rt = 0;
+ king = WhiteKing;
+ } else {
+ rf = BOARD_HEIGHT-1;
+ rt = BOARD_HEIGHT-1;
+ king = BlackKing;
+ }
+ 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 : (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;
+ }
+ if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
+ if (wom) {
+ ff = initialRights[2];
+ ft = initialRights[castlingType-1];
+ } else {
+ ff = initialRights[5];
+ ft = initialRights[castlingType+2];
+ }
+ 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%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, promo);
+ }
+ }
+
+
+ // ********* variations (nesting) ******************************
+ if(**p =='(') {
+ if(RdTime(')', p)) return ElapsedTime;
+ return Open;
+ }
+ if(**p ==')') { (*p)++; return Close; }
+ if(**p == ';') { while(**p != '\n') (*p)++; return Comment; }
+
+
+ // ********* Comments and result messages **********************
+ *p = oldp; commentEnd = NULL; result = 0;
+ if(**p == '{') {
+ if(RdTime('}', p)) return ElapsedTime;
+ if(lastChar == '\n' && Match("--------------\n", p)) {
+ char *q;
+ i = Scan ('}', p); q = *p - 16;
+ if(Match("\n--------------}\n", &q)) return PositionDiagram;
+ } else i = Scan('}', p);
+ commentEnd = *p; if(i) return Comment; // return comment that runs to EOF immediately
+ }
+ if(commentEnd) SkipWhite(p);
+ if(Match("*", p)) result = GameUnfinished;
+ else if(**p == '0') {
+ if( Match("0-1", p) || Match("0/1", p) || Match("0:1", p) ||
+ Match("0 - 1", p) || Match("0 / 1", p) || Match("0 : 1", p)) result = BlackWins;
+ } else if(**p == '1') {
+ if( Match("1-0", p) || Match("1/0", p) || Match("1:0", p) ||
+ Match("1 - 0", p) || Match("1 / 0", p) || Match("1 : 0", p)) result = WhiteWins;
+ else if(Match("1/2 - 1/2", p) || Match("1/2:1/2", p) || Match("1/2 : 1/2", p) || Match("1 / 2 - 1 / 2", p) ||
+ Match("1 / 2 : 1 / 2", p) || Match("1/2", p) || Match("1 / 2", p)) result = GameIsDrawn;
+ }
+ if(result) {
+ if(Match(" (", p) && !Scan(')', p) || Match(" {", p) && !Scan('}', p)) { // there is a comment after the PGN result!
+ if(commentEnd) { *p = commentEnd; return Comment; } // so comment before it is normal comment; return that first
+ }
+ return result; // this returns a possible preceeding comment as result details
+ }
+ if(commentEnd) { *p = commentEnd; return Comment; } // there was no PGN result following, so return as normal comment
+
+
+ // ********* Move numbers (after castlings or PGN results!) ***********
+ if((i = Number(p)) != BADNUMBER) { // a single number was read as part of our attempt to read a move
+ char *numEnd = *p;
+ if(**p == '.') (*p)++; SkipWhite(p);
+ if(**p == '+' || isalpha(**p) || gameInfo.variant == VariantShogi && *p != numEnd && isdigit(**p)) {
+ *p = numEnd;
+ return i == 1 ? MoveNumberOne : Nothing;
+ }
+ *p = numEnd; return Nothing;
+ }
+
+
+ // ********* non-compliant game-result indicators *********************
+ if(Match("+-+", p) || Word("stalemate", p)) return GameIsDrawn;
+ if(Match("++", p) || Verb("resign", p) || (Word("check", p) || 1) && Word("mate", p) )
+ return (wom ? BlackWins : WhiteWins);
+ c = ToUpper(**p);
+ if(Word("w", p) && (Match("hite", p) || 1) || Word("b", p) && (Match("lack", p) || 1) ) {
+ if(**p != ' ') return Nothing;
+ ++*p;
+ if(Verb("disconnect", p)) return GameUnfinished;
+ if(Verb("resign", p) || Verb("forfeit", p) || Word("mated", p) || Word("lost", p) || Word("loses", p))
+ return (c == 'W' ? BlackWins : WhiteWins);
+ if(Word("mates", p) || Word("wins", p) || Word("won", p))
+ return (c != 'W' ? BlackWins : WhiteWins);
+ return Nothing;
+ }
+ if(Word("draw", p)) {
+ if(**p == 'n') (*p)++;
+ if(**p != ' ') return GameIsDrawn;
+ oldp = ++*p;
+ if(Word("agreed", p)) return GameIsDrawn;
+ if(Match("by ", p) && (Word("repetition", p) || Word("agreement", p)) ) return GameIsDrawn;
+ *p = oldp;
+ if(*(*p)++ == '(') {
+ while(**p != '\n') if(*(*p)++ == ')') break;
+ if((*p)[-1] == ')') return GameIsDrawn;
+ }
+ *p = oldp - 1; return GameIsDrawn;
+ }
+
+
+ // ********* Numeric annotation glyph **********************************
+ if(**p == '$') { (*p)++; if(Number(p) != BADNUMBER) return NAG; return Nothing; }
+
+
+ // ********** by now we are getting down to the silly stuff ************
+ if(Word("gnu", p) || Match("GNU", p)) {
+ if(**p == ' ') (*p)++;
+ if(Word("chess", p) || Match("CHESS", p)) {
+ char *q;
+ if((q = strstr(*p, "game")) || (q = strstr(*p, "GAME")) || (q = strstr(*p, "Game"))) {
+ (*p) = q + 4; return GNUChessGame;
+ }
+ }
+ return Nothing;
+ }
+ if(lastChar == '\n' && (Match("# ", p) || Match("; ", p) || Match("% ", p))) {
+ while(**p != '\n' && **p != ' ') (*p)++;
+ if(**p == ' ' && (Match(" game file", p) || Match(" position file", p))) {
+ while(**p != '\n') (*p)++; // skip to EOLN
+ return XBoardGame;
+ }
+ *p = oldp; // we might need to re-match the skipped stuff
+ }
+
+ if(Match("@@@@", p) || Match("--", p) || Match("Z0", p) || Match("pass", p) || Match("null", p)) {
+ strncpy(currentMoveString, "@@@@", 5);
+ return yyboardindex & F_WHITE_ON_MOVE ? WhiteDrop : BlackDrop;
+ }
+
+ // ********* Efficient skipping of (mostly) alphabetic chatter **********
+ while(isdigit(**p) || isalpha(**p) || **p == '-') (*p)++;
+ if(*p != oldp) {
+ if(**p == '\'') {
+ while(isdigit(**p) || isalpha(**p) || **p == '-' || **p == '\'') (*p)++;
+ return Nothing; // random word
+ }
+ if(lastChar == '\n' && Match(": ", p)) { // mail header, skip indented lines
+ do {
+ while(**p != '\n') (*p)++;
+ if(!ReadLine()) return Nothing; // append next line if not EOF
+ } while(Match("\n ", p) || Match("\n\t", p));
+ }
+ return Nothing;
+ }
+
+
+ // ********* Could not match to anything. Return offending character ****
+ (*p)++;
+ return Nothing;
+}
+
+/*
+ Return offset of next pattern in the current file.
+*/
+int yyoffset()
+{
+ return ftell(inputFile) - (inPtr - parsePtr); // subtract what is read but not yet parsed
+}
+
+void yynewfile (FILE *f)
+{ // prepare parse buffer for reading file
+ inputFile = f;
+ inPtr = parsePtr = inputBuf;
+ fromString = 0;
+ lastChar = '\n';
+ *inPtr = NULLCHAR; // make sure we will start by reading a line
+}
+
+void yynewstr P((char *s))
+{
+ parsePtr = s;
+ inputFile = NULL;
+ fromString = 1;
+}
+
+int yylex()
+{ // this replaces the flex-generated parser
+ int result = NextUnit(&parsePtr);
+ char *p = parseStart, *q = yytext;
+ while(p < parsePtr) *q++ = *p++; // copy the matched text to yytext[]
+ *q = NULLCHAR;
+ lastChar = q[-1];
+ return result;
+}
+
+int Myylex()
+{ // [HGM] wrapper for yylex, which treats nesting of parentheses
+ int symbol, nestingLevel = 0, i=0;
+ char *p;
+ static char buf[256*MSG_SIZ];
+ buf[0] = NULLCHAR;
+ do { // eat away anything not at level 0
+ symbol = yylex();
+ if(symbol == Open) nestingLevel++;
+ if(nestingLevel) { // save all parsed text between (and including) the ()
+ for(p=yytext; *p && i<256*MSG_SIZ-2;) buf[i++] = *p++;
+ buf[i] = NULLCHAR;
+ }
+ if(symbol == 0) break; // ran into EOF
+ if(symbol == Close) symbol = Comment, nestingLevel--;
+ } while(nestingLevel || symbol == Nothing);
+ yy_text = buf[0] ? buf : (char*)yytext;
+ return symbol;
+}
+
+ChessMove yylexstr(int boardIndex, char *s, char *buf, int buflen)
+{
+ ChessMove ret;
+ char *savPP = parsePtr;
+ fromString = 1;
+ yyboardindex = boardIndex;
+ parsePtr = s;
+ ret = (ChessMove) Myylex();
+ strncpy(buf, yy_text, buflen-1);
+ buf[buflen-1] = NULLCHAR;
+ parsePtr = savPP;
+ fromString = 0;
+ return ret;
+}
+