[LIB-9] Add SAN, NAG, move number and result PGN tokens
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / pgn / token / MoveToken.java
1 /*
2  * Copyright (c) 2018-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.pgn.token;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.hedgecode.chess.ParseException;
23 import org.hedgecode.chess.pgn.entity.DetailMove;
24 import org.hedgecode.chess.pgn.entity.MoveNumber;
25 import org.hedgecode.chess.pgn.entity.Moves;
26 import org.hedgecode.chess.pgn.nag.Glyph;
27 import org.hedgecode.chess.pgn.token.san.CommonSANToken;
28 import org.hedgecode.chess.pgn.token.san.SANToken;
29
30 /**
31  * MoveToken
32  *
33  * @author Dmitry Samoshin aka gotty
34  */
35 public class MoveToken implements Token<Moves> {
36
37     public static final String MOVE_REGEX = "^\\s*([PNBRQKa-hx1-8=+#\\-O!?]+)(\\s+\\$([0-9]+))?";
38     private static final Pattern MOVE_PATTERN = Pattern.compile(MOVE_REGEX);
39     private static final int MOVE_GROUP = 1, NAG_GROUP = 3;
40
41     private final Token<MoveNumber> moveNumberToken = new MoveNumberToken();
42     private final SANToken moveSanToken = new CommonSANToken();
43
44     @Override
45     public int token(Moves moves, String pgn) throws ParseException {
46         MoveNumber moveNumber = new MoveNumber(
47                 moves.currentMove().ply()
48         );
49         int numberLength = moveNumberToken.token(moveNumber, pgn);
50         if (numberLength > 0) {
51             pgn = pgn.substring(numberLength);
52         }
53         Matcher matcher = MOVE_PATTERN.matcher(pgn);
54         if (!matcher.find()) {
55             throw new ParseException("parse.pgn.incorrect.move", pgn);
56         } else {
57             String move = matcher.group(MOVE_GROUP);
58             moveSanToken.token(move);
59             DetailMove detailMove = new DetailMove(
60                     moveNumber.ply(), move
61             );
62             moves.addMove(
63                     detailMove
64             );
65             String glyph = matcher.group(NAG_GROUP); // todo: more then one NAG
66             if (glyph != null) {
67                 detailMove.addGlyph(
68                         Glyph.byNumber(
69                                 Integer.parseInt(glyph)
70                         )
71                 );
72             }
73         }
74         return numberLength + matcher.group().length();
75     }
76
77 }