X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=parser.c;h=06267a3df8a2fa649fdbba4631150390c1ccb8a6;hb=c245d8f55ee094707771e5225dd91564ded4630c;hp=0a6549ef1325264ffab2777c31e59e95b8dd019f;hpb=fa7d7709b3b69ab3f5fbea393f00d6d89b13b3a1;p=xboard.git diff --git a/parser.c b/parser.c index 0a6549e..06267a3 100644 --- a/parser.c +++ b/parser.c @@ -73,7 +73,7 @@ int SkipWhite(char **p) return *p != start; } -int Match(char *pattern, char **ptr) +inline int Match(char *pattern, char **ptr) { char *p = pattern, *s = *ptr; while(*p && (*p == *s++ || s[-1] == '\r' && *p--)) p++; @@ -84,7 +84,7 @@ int Match(char *pattern, char **ptr) return 0; // no match, no ptr update } -int Word(char *pattern, char **p) +inline int Word(char *pattern, char **p) { if(Match(pattern, p)) return 1; if(*pattern >= 'a' && *pattern <= 'z' && *pattern - **p == 'a' - 'A') { // capitalized @@ -133,7 +133,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 +144,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; } @@ -157,39 +157,6 @@ int NextUnit(char **p) } parseStart = oldp = *p; // remember where we begin - // Next we do some common symbols where the first character commits us to things that cannot possibly be a move - // (but not {} comments, as those force time-consuming matching of PGN results immediately after it) - - // ********* 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; - } - - - // ********* variations (nesting) ****************************** - if(**p =='(') { - if(RdTime(')', p)) return ElapsedTime; - return Open; - } - if(**p ==')') { (*p)++; return Close; } - if(**p == ';') { while(**p != '\n') (*p)++; return Comment; } - // ********* attempt to recognize a SAN move in the leading non-blank text ***** piece = separator = promoted = slash = n = 0; @@ -247,7 +214,7 @@ if(appData.debugMode)fprintf(debugFP, "trial %d,%d,%d,%d type %d%d%d%d\n", coor && (coord[0] != 14 || coord[2] != 14) /* reserve oo for castling! */ ) { 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 @@ -339,10 +306,34 @@ if(appData.debugMode)fprintf(debugFP, "trial %d,%d,%d,%d type %d%d%d%d\n", coor } } 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 == '"') { + (*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 ************************************* - *p = oldp; 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) || @@ -351,6 +342,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 */ @@ -366,7 +360,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; @@ -382,16 +376,25 @@ 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); } } + // ********* 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 == '{') { @@ -489,6 +492,10 @@ badMove:// we failed to find algebraic move *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)++;