-// New PGN parser by by HGM. I was dissatisfied with the old flex-generated parser for several reasons:\r
-// 1) It required flex to build\r
-// 2) It was not possible to use variant-dependent syntax, which gave trouble for '+' as Sogi promoChar vs check symbol\r
-// 3) It could not handle double-digit rank numbers\r
-// 4) It could not handle PSN moves, with (alpha rank and file digit)\r
-// 5) Having more than 12 ranks would require extension of the rules anyway\r
-// 6) It was cumbersome to maintain, which much code duplication that had to be kept in sync when changing something\r
-// 7) It needed special handling for packaging, because we wanted to include parser.c for people who had no flex\r
-// 8) It was quite large because of the table-driven flex algorithm.\r
-// This new parser suffers from none of that. It might even accomodate traditional Xiangqi notation at some future time.\r
-\r
-#include "config.h"\r
-#include <stdio.h>\r
-#include <ctype.h>\r
-#include <string.h>\r
-#include "common.h"\r
-#include "backend.h"\r
-#include "frontend.h"\r
-#include "parser.h"\r
-#include "moves.h"\r
-\r
-\r
-extern Board boards[MAX_MOVES];\r
-extern int PosFlags(int nr);\r
-int yyboardindex;\r
-int yyskipmoves = FALSE;\r
-char currentMoveString[4096]; // a bit ridiculous size?\r
-char *yy_text;\r
-\r
-#define PARSEBUFSIZE 10000\r
-\r
-static FILE *inputFile;\r
-static char *inPtr, *parsePtr, *parseStart;\r
-static char inputBuf[PARSEBUFSIZE];\r
-static char yytext[PARSEBUFSIZE];\r
-static char fromString = 0, lastChar = '\n';\r
-\r
-#define NOTHING 0\r
-#define NUMERIC 1\r
-#define ALPHABETIC 2\r
-#define BADNUMBER (-2000000000)\r
-\r
-int ReadLine()\r
-{ // Read one line from the input file, and append to the buffer\r
- char c, *start = inPtr;\r
- if(fromString) return 0; // parsing string, so the end is a hard end\r
- if(!inputFile) return 0;\r
- while((c = fgetc(inputFile)) != EOF) {\r
- *inPtr++ = c;\r
- if(c == '\n') { *inPtr = NULLCHAR; return 1; }\r
- if(inPtr - inputBuf > PARSEBUFSIZE-2) inPtr--; //prevent crash on overflow\r
- }\r
- if(inPtr == start) return 0;\r
- *inPtr++ = '\n', *inPtr = NULLCHAR; // repair missing linefeed at EOF\r
- return 1;\r
-}\r
-\r
-int Scan(char c, char **p)\r
-{ // line-spanning skip to mentioned character or EOF\r
- do {\r
- while(**p) if(*(*p)++ == c) return 0;\r
- } while(ReadLine());\r
- // no closing bracket; force match for entire rest of file.\r
- return 1;\r
-}\r
-\r
-int SkipWhite(char **p)\r
-{ // skip spaces tabs and newlines; return 1 if anything was skipped\r
- char *start = *p;\r
- do{\r
- while(**p == ' ' || **p == '\t' || **p == '\n' || **p == '\r') (*p)++;\r
- } while(**p == NULLCHAR && ReadLine()); // continue as long as ReadLine reads something\r
- return *p != start;\r
-}\r
-\r
-int Match(char *pattern, char **ptr)\r
-{\r
- char *p = pattern, *s = *ptr;\r
- while(*p && (*p == *s++ || s[-1] == '\r' && *p--)) p++;\r
- if(*p == 0) {\r
- *ptr = s;\r
- return 1;\r
- }\r
- return 0; // no match, no ptr update\r
-}\r
-\r
-int Word(char *pattern, char **p)\r
-{\r
- if(Match(pattern, p)) return 1;\r
- if(*pattern >= 'a' && *pattern <= 'z' && *pattern - **p == 'a' - 'A') { // capitalized\r
- (*p)++;\r
- if(Match(pattern + 1, p)) return 1;\r
- (*p)--;\r
- }\r
- return 0;\r
-}\r
-\r
-int Verb(char *pattern, char **p)\r
-{\r
- int res = Word(pattern, p);\r
- if(res && !Match("s", p)) Match("ed", p); // eat conjugation suffix, if any\r
- return res;\r
-}\r
-\r
-\r
-int Number(char **p)\r
-{\r
- int val = 0;\r
- if(**p < '0' || **p > '9') return BADNUMBER;\r
- while(**p >= '0' && **p <= '9') {\r
- val = 10*val + *(*p)++ - '0';\r
- }\r
- return val;\r
-}\r
-\r
-int RdTime(char c, char **p)\r
-{\r
- char *start = ++(*p), *sec; // increment *p, as it was pointing to the opening ( or {\r
- if(Number(p) == BADNUMBER) return 0;\r
- sec = *p;\r
- if(Match(":", p) && Number(p) != BADNUMBER && *p - sec == 3) { // well formed\r
- sec = *p;\r
- if(Match(".", p) && Number(p) != BADNUMBER && *(*p)++ == c) return 1; // well-formed fraction\r
- *p = sec;\r
- if(*(*p)++ == c) return 1; // matching bracket without fraction\r
- }\r
- *p = start; // failure\r
- return 0;\r
-}\r
-\r
-char PromoSuffix(char **p)\r
-{\r
- char *start = *p;\r
- if(**p == 'e' && (Match("ep", p) || Match("e.p.", p))) { *p = start; return NULLCHAR; } // non-compliant e.p. suffix is no promoChar!\r
- if(**p == '+' && gameInfo.variant == VariantShogi) { (*p)++; return '+'; } \r
- if(**p == '=') (*p)++; //optional =\r
- if(**p == '(' && (*p)[2] == ')' && isalpha( (*p)[1] )) { (*p) += 3; return (*p)[-2]; }\r
- if(isalpha(**p)) return *(*p)++;\r
- if(*p != start) return '='; // must be the optional =\r
- return NULLCHAR; // no suffix detected\r
-}\r
-\r
-int NextUnit(char **p)\r
-{ // Main parser routine\r
- int coord[4], n, result, piece, i;\r
- char type[4], promoted, separator, slash, *oldp, *commentEnd, c;\r
- int wom = WhiteOnMove(yyboardindex);\r
-\r
- // ********* try white first, because it is so common **************************\r
- if(**p == ' ' || **p == '\n' || **p == '\t') { parseStart = (*p)++; return Nothing; }\r
-\r
-\r
- if(**p == NULLCHAR) { // make sure there is something to parse\r
- if(fromString) return 0; // we are parsing string, so the end is really the end\r
- *p = inPtr = inputBuf;\r
- if(!ReadLine()) return 0; // EOF\r
- }\r
- parseStart = oldp = *p; // remember where we begin\r
-\r
- // Next we do some common symbols where the first character commits us to things that cannot possibly be a move\r
- // (but not {} comments, as those force time-consuming matching of PGN results immediately after it)\r
-\r
- // ********* PGN tags ******************************************\r
- if(**p == '[') {\r
- oldp = ++(*p);\r
- if(Match("--", p)) { // "[--" could be start of position diagram\r
- if(!Scan(']', p) && (*p)[-3] == '-' && (*p)[-2] == '-') return PositionDiagram; \r
- *p = oldp;\r
- }\r
- SkipWhite(p);\r
- if(isdigit(**p) || isalpha(**p)) {\r
- do (*p)++; while(isdigit(**p) || isalpha(**p) || **p == '+' ||\r
- **p == '-' || **p == '=' || **p == '_' || **p == '#');\r
- SkipWhite(p);\r
- if(*(*p)++ == '"') {\r
- while(**p != '\n' && (*(*p)++ != '"'|| (*p)[-2] == '\\')); // look for unescaped quote\r
- if((*p)[-1] !='"') { *p = oldp; Scan(']', p); return Comment; } // string closing delimiter missing\r
- SkipWhite(p); if(*(*p)++ == ']') return PGNTag;\r
- }\r
- }\r
- Scan(']', p); return Comment;\r
- }\r
-\r
-\r
- // ********* variations (nesting) ******************************\r
- if(**p =='(') {\r
- if(RdTime(')', p)) return ElapsedTime;\r
- return Open;\r
- }\r
- if(**p ==')') { (*p)++; return Close; }\r
- if(**p == ';') { while(**p != '\n') (*p)++; return Comment; }\r
-\r
-\r
- // ********* attempt to recognize a SAN move in the leading non-blank text *****\r
- piece = separator = promoted = slash = n = 0;\r
- for(i=0; i<4; i++) coord[i] = -1, type[i] = NOTHING;\r
- if(**p == '+') (*p)++, promoted++;\r
- if(**p >= 'A' && **p <= 'Z') {\r
- piece = *(*p)++; // Note we could test for 2-byte non-ascii names here\r
- if(**p == '/') slash = *(*p)++;\r
- }\r
- while(n < 4) {\r
- if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC;\r
- else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;\r
- else break;\r
- if(n == 2 && type[0] == type[1]) { // if two identical types, the opposite type in between must have been missing\r
- type[2] = type[1]; coord[2] = coord[1];\r
- type[1] = NOTHING; coord[1] = -1; n++;\r
- }\r
- }\r
- // we always get here, and might have read a +, a piece, and upto 4 potential coordinates\r
- if(n <= 2) { // could be from-square or disambiguator, when -:xX follow, or drop with @ directly after piece, but also to-square\r
- if(**p == '-' || **p == ':' || **p == 'x' || **p == 'X' || // these cannot be move suffix, so to-square must follow\r
- (**p == '@' || **p == '*') && n == 0 && !promoted && piece) { // P@ must also be followed by to-square\r
- separator = *(*p)++;\r
- if(n == 1) coord[1] = coord[0]; // must be disambiguator, but we do not know which yet\r
- n = 2;\r
- while(n < 4) { // attempt to read to-square\r
- if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC;\r
- else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;\r
- else break;\r
- }\r
- } else if((**p == '+' || **p == '=') && n == 1 && piece && type[0] == NUMERIC) { // can be traditional Xiangqi notation\r
- separator = *(*p)++;\r
- n = 2;\r
- if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;\r
- } else if(n == 2) { // only one square mentioned, must be to-square\r
- while(n < 4) { coord[n] = coord[n-2], type[n] = type[n-2], coord[n-2] = -1, type[n-2] = NOTHING; n++; }\r
- }\r
- } else if(n == 3 && type[1] != NOTHING) { // must be hyphenless disambiguator + to-square\r
- for(i=3; i>0; i--) coord[i] = coord[i-1], type[i] = type[i-1]; // move to-square to where it belongs\r
- type[1] = NOTHING; // disambiguator goes in first two positions\r
- n = 4;\r
- }\r
-if(appData.debugMode)fprintf(debugFP, "trial %d,%d,%d,%d type %d%d%d%d\n", coord[0], coord[1], coord[2], coord[3], type[0], type[1], type[2], type[3]);\r
- // we always get here; move must be completely read now, with to-square coord(s) at end\r
- if(n == 3) { // incomplete to-square. Could be Xiangqi traditional, or stuff like fxg\r
- if(piece && type[1] == NOTHING && type[0] == NUMERIC && type[2] == NUMERIC && \r
- (separator == '+' || separator == '=' || separator == '-')) {\r
- // Xiangqi traditional\r
-\r
- return ImpossibleMove; // for now treat as invalid\r
- }\r
- // fxg stuff, but also things like 0-0, 0-1 and 1-0\r
+// New PGN parser by by HGM. I was dissatisfied with the old flex-generated parser for several reasons:
+// 1) It required flex to build
+// 2) It was not possible to use variant-dependent syntax, which gave trouble for '+' as Sogi promoChar vs check symbol
+// 3) It could not handle double-digit rank numbers
+// 4) It could not handle PSN moves, with (alpha rank and file digit)
+// 5) Having more than 12 ranks would require extension of the rules anyway
+// 6) It was cumbersome to maintain, which much code duplication that had to be kept in sync when changing something
+// 7) It needed special handling for packaging, because we wanted to include parser.c for people who had no flex
+// 8) It was quite large because of the table-driven flex algorithm.
+// This new parser suffers from none of that. It might even accomodate traditional Xiangqi notation at some future time.
+
+#include "config.h"
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include "common.h"
+#include "backend.h"
+#include "frontend.h"
+#include "parser.h"
+#include "moves.h"
+
+
+extern Board boards[MAX_MOVES];
+extern int PosFlags(int nr);
+int yyboardindex;
+int yyskipmoves = FALSE;
+char currentMoveString[4096]; // a bit ridiculous size?
+char *yy_text;
+
+#define PARSEBUFSIZE 10000
+
+static FILE *inputFile;
+static char *inPtr, *parsePtr, *parseStart;
+static char inputBuf[PARSEBUFSIZE];
+static char yytext[PARSEBUFSIZE];
+static char fromString = 0, lastChar = '\n';
+
+#define NOTHING 0
+#define NUMERIC 1
+#define ALPHABETIC 2
+#define BADNUMBER (-2000000000)
+
+int ReadLine()
+{ // Read one line from the input file, and append to the buffer
+ char c, *start = inPtr;
+ if(fromString) return 0; // parsing string, so the end is a hard end
+ if(!inputFile) return 0;
+ while((c = fgetc(inputFile)) != EOF) {
+ *inPtr++ = c;
+ if(c == '\n') { *inPtr = NULLCHAR; return 1; }
+ if(inPtr - inputBuf > PARSEBUFSIZE-2) inPtr--; //prevent crash on overflow
+ }
+ if(inPtr == start) return 0;
+ *inPtr++ = '\n', *inPtr = NULLCHAR; // repair missing linefeed at EOF
+ return 1;
+}
+
+int Scan(char c, char **p)
+{ // line-spanning skip to mentioned character or EOF
+ do {
+ while(**p) if(*(*p)++ == c) return 0;
+ } while(ReadLine());
+ // no closing bracket; force match for entire rest of file.
+ return 1;
+}
+
+int SkipWhite(char **p)
+{ // skip spaces tabs and newlines; return 1 if anything was skipped
+ char *start = *p;
+ do{
+ while(**p == ' ' || **p == '\t' || **p == '\n' || **p == '\r') (*p)++;
+ } while(**p == NULLCHAR && ReadLine()); // continue as long as ReadLine reads something
+ return *p != start;
+}
+
+int Match(char *pattern, char **ptr)
+{
+ char *p = pattern, *s = *ptr;
+ while(*p && (*p == *s++ || s[-1] == '\r' && *p--)) p++;
+ if(*p == 0) {
+ *ptr = s;
+ return 1;
+ }
+ return 0; // no match, no ptr update
+}
+
+int Word(char *pattern, char **p)
+{
+ if(Match(pattern, p)) return 1;
+ if(*pattern >= 'a' && *pattern <= 'z' && *pattern - **p == 'a' - 'A') { // capitalized
+ (*p)++;
+ if(Match(pattern + 1, p)) return 1;
+ (*p)--;
+ }
+ return 0;
+}
+
+int Verb(char *pattern, char **p)
+{
+ int res = Word(pattern, p);
+ if(res && !Match("s", p)) Match("ed", p); // eat conjugation suffix, if any
+ return res;
+}
+
+
+int Number(char **p)
+{
+ int val = 0;
+ if(**p < '0' || **p > '9') return BADNUMBER;
+ while(**p >= '0' && **p <= '9') {
+ val = 10*val + *(*p)++ - '0';
+ }
+ return val;
+}
+
+int RdTime(char c, char **p)
+{
+ char *start = ++(*p), *sec; // increment *p, as it was pointing to the opening ( or {
+ if(Number(p) == BADNUMBER) return 0;
+ sec = *p;
+ if(Match(":", p) && Number(p) != BADNUMBER && *p - sec == 3) { // well formed
+ sec = *p;
+ if(Match(".", p) && Number(p) != BADNUMBER && *(*p)++ == c) return 1; // well-formed fraction
+ *p = sec;
+ if(*(*p)++ == c) return 1; // matching bracket without fraction
+ }
+ *p = start; // failure
+ return 0;
+}
+
+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 == '(' && (*p)[2] == ')' && isalpha( (*p)[1] )) { (*p) += 3; return (*p)[-2]; }
+ if(isalpha(**p)) return *(*p)++;
+ if(*p != start) return '='; // must be the optional =
+ return NULLCHAR; // no suffix detected
+}
+
+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);
+
+ // ********* try white first, because it is so common **************************
+ if(**p == ' ' || **p == '\n' || **p == '\t') { parseStart = (*p)++; return Nothing; }
+
+
+ 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;
+ if(!ReadLine()) return 0; // EOF
+ }
+ 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;
+ for(i=0; i<4; i++) coord[i] = -1, type[i] = NOTHING;
+ if(**p == '+') (*p)++, promoted++;
+ if(**p >= 'A' && **p <= 'Z') {
+ piece = *(*p)++; // Note we could test for 2-byte non-ascii names here
+ if(**p == '/') slash = *(*p)++;
+ }
+ 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;
+ else break;
+ if(n == 2 && type[0] == type[1]) { // if two identical types, the opposite type in between must have been missing
+ type[2] = type[1]; coord[2] = coord[1];
+ type[1] = NOTHING; coord[1] = -1; n++;
+ }
+ }
+ // we always get here, and might have read a +, a piece, and upto 4 potential coordinates
+ if(n <= 2) { // could be from-square or disambiguator, when -:xX follow, or drop with @ directly after piece, but also to-square
+ if(**p == '-' || **p == ':' || **p == 'x' || **p == 'X' || // these cannot be move suffix, so to-square must follow
+ (**p == '@' || **p == '*') && n == 0 && !promoted && piece) { // P@ must also be followed by to-square
+ separator = *(*p)++;
+ if(n == 1) coord[1] = coord[0]; // must be disambiguator, but we do not know which yet
+ n = 2;
+ while(n < 4) { // attempt to read to-square
+ if(**p >= 'a' && **p < 'x') coord[n] = *(*p)++ - 'a', type[n++] = ALPHABETIC;
+ else if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;
+ else break;
+ }
+ } else if((**p == '+' || **p == '=') && n == 1 && piece && type[0] == NUMERIC) { // can be traditional Xiangqi notation
+ separator = *(*p)++;
+ n = 2;
+ if((i = Number(p)) != BADNUMBER) coord[n] = i, type[n++] = NUMERIC;
+ } else if(n == 2) { // only one square mentioned, must be to-square
+ while(n < 4) { coord[n] = coord[n-2], type[n] = type[n-2], coord[n-2] = -1, type[n-2] = NOTHING; n++; }
+ }
+ } else if(n == 3 && type[1] != NOTHING) { // must be hyphenless disambiguator + to-square
+ for(i=3; i>0; i--) coord[i] = coord[i-1], type[i] = type[i-1]; // move to-square to where it belongs
+ type[1] = NOTHING; // disambiguator goes in first two positions
+ n = 4;
+ }
+if(appData.debugMode)fprintf(debugFP, "trial %d,%d,%d,%d type %d%d%d%d\n", coord[0], coord[1], coord[2], coord[3], type[0], type[1], type[2], type[3]);
+ // we always get here; move must be completely read now, with to-square coord(s) at end
+ if(n == 3) { // incomplete to-square. Could be Xiangqi traditional, or stuff like fxg
+ if(piece && type[1] == NOTHING && type[0] == NUMERIC && type[2] == NUMERIC &&
+ (separator == '+' || separator == '=' || separator == '-')) {
+ // Xiangqi traditional
+
+ return ImpossibleMove; // for now treat as invalid
+ }
+ // fxg stuff, but also things like 0-0, 0-1 and 1-0
if(!piece && type[1] == NOTHING && type[0] == ALPHABETIC && type[2] == ALPHABETIC
&& (coord[0] != 14 || coord[2] != 14) /* reserve oo for castling! */ ) {
- 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
+ }
+ }
+ 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
+
+
+ // ********* 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) ||
+ 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;
+
+ 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 : 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",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
+ 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);
+ }
+ }
+
+
+ // ********* 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
+ }
+
+
+ // ********* 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;
+}