New parser, written in C
[xboard.git] / parser.l
index 48b94ff..8ceaa55 100644 (file)
--- a/parser.l
+++ b/parser.l
@@ -12,7 +12,7 @@
  * Massachusetts.
  *
  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005,
- * 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+ * 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  *
  * The following terms apply to Digital Equipment Corporation's copyright
  * interest in XBoard:
@@ -620,7 +620,7 @@ extern void CopyBoard P((Board to, Board from));
     cl.ftIn = yytext[1+skip] - AAA;
     cl.promoCharIn = NULLCHAR;
 
-    if(yyleng-skip > 3 && (gameInfo.variant == VariantShogi || gameInfo.variant == VariantSChess)) /* [HGM] can have Shogi-style promotion */
+    if(yyleng-skip > 3) /* [HGM] in some variants pieces promote */
         cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
     if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
 
@@ -687,7 +687,7 @@ extern void CopyBoard P((Board to, Board from));
     cl.ftIn = yytext[2+skip] - AAA;
     cl.promoCharIn = NULLCHAR;
 
-    if(yyleng-skip > 4 && (gameInfo.variant == VariantShogi || gameInfo.variant == VariantSChess)) /* [HGM] can have Shogi-style promotion */
+    if(yyleng-skip > 4) /* [HGM] in some variants pieces promote */
         cl.promoCharIn = yytext[yyleng-1-(yytext[yyleng-1]==')')];
     if(cl.promoCharIn == '+' && gameInfo.variant != VariantShogi) cl.promoCharIn = NULLCHAR; // + means check outside Shogi
 
@@ -756,7 +756,7 @@ extern void CopyBoard P((Board to, Board from));
         {
           fprintf(debugFP, "Parser FRC long %d %d\n", ff, ft);
         };
-        if(ff < 0 || ft < 0) return 0;
+        if(ff == NoRights || ft == NoRights) return ImpossibleMove;
     }
     sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
     if (appData.debugMode) {
@@ -810,7 +810,7 @@ extern void CopyBoard P((Board to, Board from));
     if (appData.debugMode) {
         fprintf(debugFP, "Parser FRC short %d %d\n", ff, ft);
     }
-        if(ff < 0 || ft < 0) return 0;
+        if(ff == NoRights || ft == NoRights) return ImpossibleMove;
     }
     sprintf(currentMoveString, "%c%c%c%c",ff+AAA,rf+ONE,ft+AAA,rt+ONE);
     if (appData.debugMode) {
@@ -823,6 +823,9 @@ extern void CopyBoard P((Board to, Board from));
 }
 
 [A-Za-z][@*][a-l][0-9] {
+
+    if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
+
     /* Bughouse piece drop. */
     currentMoveString[1] = '@';
     currentMoveString[2] = yytext[2];
@@ -842,6 +845,8 @@ extern void CopyBoard P((Board to, Board from));
     } else {
        currentMoveString[0] = ToLower(yytext[0]);
     }
+    if(CharToPiece(currentMoveString[0]) == EmptySquare) return ImpossibleMove; // Unknown piece;
+
     return LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), DROP_RANK, // [HGM] does drops now too
                         CharToPiece(currentMoveString[0]), currentMoveString[3] - ONE, currentMoveString[2] - AAA, NULLCHAR);
 }
@@ -899,19 +904,19 @@ extern void CopyBoard P((Board to, Board from));
     return (int) (ToUpper(yytext[0]) == 'W' ? BlackWins : WhiteWins);
 }
 
-("{"[^\}\n]*"} ")?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? { 
+("{"[^\}]*"}"[ \n])?(1-0|"1 - 0"|"1/0"|"1 / 0"|"1:0"|"1 : 0")(" (".*")"|" {".*"}")? { 
     return (int) WhiteWins;
 }
 
-("{"[^\}\n]*"} ")?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? { 
+("{"[^\}]*"}"[ \n])?(0-1|"0 - 1"|"0/1"|"0 / 1"|"0:1"|"0 : 1")(" (".*")"|" {".*"}")? { 
     return (int) BlackWins;
 }
 
-("{"[^\}\n]*"} ")?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
+("{"[^\}]*"}"[ \n])?("1/2"|"1 / 2")(" "?[-:]" "?("1/2"|"1 / 2"))?(" (".*")"|" {".*"}")? {
     return (int) GameIsDrawn;
 }
 
-("{"[^\}\n]*"} ")?"*"(" (".*")"|" {".*"}")? {
+("{"[^\}]*"}"[ \n])?"*"(" (".*")"|" {".*"}")? {
     return (int) GameUnfinished;
 }