From d6a5884d9a6af414ed26d5b70a1b3dcf131b1e9c Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Fri, 19 Feb 2010 11:18:21 +0100 Subject: [PATCH] Let yy_text determine progress of PV parsing The parsed unit yy_text was not available after ParseOneMove(), because the buffer switch in yylexstr() apparently destroys it. To solve that, yylexstr() now is equiped with an extra buffer argument, where it copies yy_text before the switch, so the caller, ParsePV(), can see what was parsed in yy_textstr. This is used to update the PV pointer, but also to save any parsed comments (when ParsePV is used for parsing a PGN variation). --- backend.c | 39 ++++++++++++--------------------------- parser.h | 2 +- parser.l | 12 +++++++----- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/backend.c b/backend.c index 424463d..18d48ce 100644 --- a/backend.c +++ b/backend.c @@ -4715,6 +4715,8 @@ AlphaRank(char *move, int n) } } +char yy_textstr[8000]; + /* Parser for moves from gnuchess, ICS, or user typein box */ Boolean ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar) @@ -4727,7 +4729,7 @@ ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar) if (appData.debugMode) { fprintf(debugFP, "move to parse: %s\n", move); } - *moveType = yylexstr(moveNum, move); + *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr); switch (*moveType) { case WhitePromotionChancellor: @@ -4821,15 +4823,15 @@ ParsePV(char *pv, Boolean storeComments) int fromX, fromY, toX, toY; char promoChar; ChessMove moveType; Boolean valid; - int nr = 0, dummy; + int nr = 0; endPV = forwardMostMove; do { - while(*pv == ' ') pv++; - if(nr == 0 && *pv == '(') pv++; // first (ponder) move can be in parentheses + while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace + if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar); if(appData.debugMode){ -fprintf(debugFP,"parsePV: %d %c%c%c%c '%s'\n", valid, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, pv); +fprintf(debugFP,"parsePV: %d %c%c%c%c yy='%s'\nPV = '%s'\n", valid, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, yy_textstr, pv); } if(!valid && nr == 0 && ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){ @@ -4848,30 +4850,13 @@ fprintf(debugFP,"parsePV: %d %c%c%c%c '%s'\n", valid, fromX+AAA, fromY+ONE, toX+ strcpy(moveList[endPV-2], "_0_0"); // suppress premove highlight on takeback move } } - if(moveType == Comment) { - // [HGM] vari: try to skip comment - int level = 0; char c, *start = pv, wait = NULLCHAR; - do { - if(!wait) { - if(*pv == '(') level++; else - if(*pv == ')' && level) level--; - } - if(*pv == wait) wait = NULLCHAR; else - if(*pv == '{') wait = '}'; else - if(*pv == '[') wait = ']'; - pv++; - } while(*pv && (wait || level)); - if(storeComments) { - c = *pv; *pv = NULLCHAR; - AppendComment(endPV, start, FALSE); - *pv = c; - } - valid++; // allow comments in PV + pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed + if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move; + if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE); + if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) { + valid++; // allow comments in PV continue; } - if(sscanf(pv, "%d...", &dummy) == 1 || sscanf(pv, "%d.", &dummy) == 1) - while(*pv && *pv++ != ' '); // skip any move numbers - while(*pv && *pv++ != ' '); // skip what we parsed; assume space separators nr++; if(endPV+1 > framePtr) break; // no space, truncate if(!valid) break; diff --git a/parser.h b/parser.h index 3a13884..57296e1 100644 --- a/parser.h +++ b/parser.h @@ -54,7 +54,7 @@ extern void yynewfile P((FILE *f)); extern void yynewstr P((char *s)); extern int yylex P((void)); -extern ChessMove yylexstr P((int boardIndex, char *s)); +extern ChessMove yylexstr P((int boardIndex, char *s, char *buf, int buflen)); extern char currentMoveString[]; extern int yyboardindex; extern int yyskipmoves; /* If TRUE, all moves are reported as AmbiguousMove diff --git a/parser.l b/parser.l index 8e3d397..348bd15 100644 --- a/parser.l +++ b/parser.l @@ -1085,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 @@ -1104,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) -- 1.7.0.4