Let yy_text determine progress of PV parsing
authorH.G. Muller <h.g.muller@hccnet.nl>
Fri, 19 Feb 2010 10:18:21 +0000 (11:18 +0100)
committerArun Persaud <arun@nubati.net>
Sun, 21 Feb 2010 00:35:02 +0000 (16:35 -0800)
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
parser.h
parser.l

index 424463d..18d48ce 100644 (file)
--- 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;
index 3a13884..57296e1 100644 (file)
--- 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
index 8e3d397..348bd15 100644 (file)
--- a/parser.l
+++ b/parser.l
@@ -1085,10 +1085,10 @@ int yywrap()
 \r
 /* Parse a move from the given string s */\r
 /* ^ at start of pattern WON'T work here unless using flex */\r
-ChessMove yylexstr(boardIndex, s)\r
-     int boardIndex;\r
-     char *s;\r
-{\r
+ChessMove yylexstr(boardIndex, s, text, len)\r
+     int boardIndex, len;\r
+     char *s, *text;\r
+{
     ChessMove ret;\r
     char *oldStringToLex;\r
 #ifdef FLEX_SCANNER\r
@@ -1104,7 +1104,9 @@ ChessMove yylexstr(boardIndex, s)
     yy_switch_to_buffer(buffer);\r
 #endif /*FLEX_SCANNER*/\r
 \r
-    ret = (ChessMove) yylex();\r
+    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;\r
 \r
 #ifdef FLEX_SCANNER\r
     if (oldBuffer != NULL) \r