--- /dev/null
+/*
+ * Copyright (c) 2018-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.pgn.token.san;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.hedgecode.chess.ParseException;
+
+/**
+ * CommonSANToken
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class CommonSANToken implements SANToken {
+
+ private static final String COMMON = "[PNBRQK]?[a-h]?[1-8]?[x]?[a-h][1-8]";
+ private static final String CASTLING = "O-O-O|O-O";
+ private static final String PROMOTION = "=[NBRQ]";
+ private static final String CHECKMATE = "[+#]";
+ private static final String EVALUATE = "[!?]{1,2}";
+
+ private static final String[] MOVE_EVALUATE = { "!", "?", "!!", "!?", "?!", "??" };
+
+ private static final String ENDS_WITH_FORMAT = "(%s)$";
+
+ private static final Pattern COMMON_PATTERN = Pattern.compile(COMMON);
+ private static final Pattern CASTLING_PATTERN = Pattern.compile(CASTLING);
+
+ @Override
+ public int token(String san) throws ParseException {
+ String tokenSan = san;
+ int count = isEvaluate(tokenSan);
+ if (count > 0) {
+ tokenSan = cutEnd(tokenSan, count);
+ }
+ count = isCheckmate(tokenSan);
+ if (count > 0) {
+ tokenSan = cutEnd(tokenSan, count);
+ }
+ if (!isCastling(tokenSan)) {
+ count = isPromotion(tokenSan);
+ if (count > 0) {
+ tokenSan = cutEnd(tokenSan, count);
+ }
+ if (!isCommon(tokenSan)) {
+ throw new ParseException("parse.pgn.incorrect.move", san);
+ }
+ }
+ return san.length();
+ }
+
+ protected boolean isCommon(String san) {
+ return COMMON_PATTERN.matcher(san).matches();
+ }
+
+ protected boolean isCastling(String san) {
+ return CASTLING_PATTERN.matcher(san).matches();
+ }
+
+ protected int isPromotion(String san) {
+ return endsWith(san, PROMOTION);
+ }
+
+ protected int isCheckmate(String san) {
+ return endsWith(san, CHECKMATE);
+ }
+
+ protected int isEvaluate(String san) {
+ return endsWith(san, EVALUATE);
+ }
+
+ private int endsWith(String san, String regex) {
+ Matcher endsWithMatcher = Pattern.compile(
+ String.format(ENDS_WITH_FORMAT, regex)
+ ).matcher(san);
+ return endsWithMatcher.find()
+ ? endsWithMatcher.group(1).length()
+ : 0;
+ }
+
+ private String cutEnd(String san, int count) {
+ return san.substring(0, san.length() - count);
+ }
+
+}