[LIB-9] Add reserved, NAG and semicolon comment PGN tokens
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / pgn / token / MovesToken.java
index 78de96c..fd0f21c 100644 (file)
@@ -32,10 +32,13 @@ public class MovesToken<Entity extends Moves> implements Token<Entity> {
 
     enum Type {
 
-        MOVE      ( new MoveToken()      ),
-        COMMENT   ( new CommentToken()   ),
-        VARIATION ( new VariationToken() ),
-        RESULT    ( new ResultToken()    );
+        MOVE      ( new MoveToken()                               ),
+        NAG       ( new NAGToken()                                ),
+        BRACE     ( new CommentToken(CommentToken.Type.BRACE)     ),
+        SEMICOLON ( new CommentToken(CommentToken.Type.SEMICOLON) ),
+        VARIATION ( new VariationToken()                          ),
+        RESULT    ( new ResultToken()                             ),
+        RESERVED  ( new ReservedToken()                           );
 
         private Token<Moves> token;
 
@@ -50,17 +53,21 @@ public class MovesToken<Entity extends Moves> implements Token<Entity> {
 
     private static final String MOVES_REGEX = "^\\s*([^\\s])";
     private static final Pattern MOVES_PATTERN = Pattern.compile(MOVES_REGEX);
+    private static final int TYPE_GROUP = 1;
 
     private static final String RESULT_REGEX = "^\\s*(1-0|0-1|1/2-1/2|\\*)";
     private static final Pattern RESULT_PATTERN = Pattern.compile(RESULT_REGEX);
 
-    private static final String COMMENT = "{";
+    private static final String NAG = "$";
+    private static final String BRACE = "{";
+    private static final String SEMICOLON = ";";
     private static final String VARIATION = "(";
+    private static final String RESERVED = "<";
 
     @Override
     public int token(Entity entity, String pgn) throws ParseException {
         int pgnLength = pgn.length();
-        pgn = PGNUtils.stripCrlf(pgn); // todo: starting with ";" comments
+        pgn = PGNUtils.tabCrlf(pgn);
         boolean isResulted = false;
         Type tokenType = tokenType(pgn);
         while (tokenType != null) {
@@ -82,13 +89,22 @@ public class MovesToken<Entity extends Moves> implements Token<Entity> {
         } else {
             Matcher moveMatcher = MOVES_PATTERN.matcher(pgn);
             if (moveMatcher.find()) {
-                switch (moveMatcher.group(1)) {
-                    case COMMENT:
-                        type = Type.COMMENT;
+                switch (moveMatcher.group(TYPE_GROUP)) {
+                    case NAG:
+                        type = Type.NAG;
+                        break;
+                    case BRACE:
+                        type = Type.BRACE;
+                        break;
+                    case SEMICOLON:
+                        type = Type.SEMICOLON;
                         break;
                     case VARIATION:
                         type = Type.VARIATION;
                         break;
+                    case RESERVED:
+                        type = Type.RESERVED;
+                        break;
                     default:
                         type = Type.MOVE;
                 }