Esempio n. 1
0
        protected override PgnParseResult DoParse(char current, char next, PgnGame currentGame)
        {
            _current = currentGame;
            _stateBuffer.Add(current);

            return(PgnParseResult.None);
        }
Esempio n. 2
0
        internal PgnParseResult Parse(char current, char next, PgnGame currentGame)
        {
            var result = _currentState.Parse(current, next, currentGame);

            if (result == PgnParseResult.EndOfGame)
            {
                _currentState.ChangeState(_initState);
            }

            return(result);
        }
Esempio n. 3
0
        protected override PgnParseResult DoParse(char current, char next, PgnGame currentGame)
        {
            _stateBuffer.Add(current);

            if (next == PgnToken.RecursiveVariationEnd.Token)
            {
                OnExit();
                var latest = _history.Pop();
                _statemachine.SetState(latest);
            }

            return(PgnParseResult.None);
        }
Esempio n. 4
0
        public virtual PgnParseResult Parse(char current, char next, PgnGame currentGame)
        {
            if (TryTransite(current, currentGame))
            {
                return(PgnParseResult.None);
            }

            if (TryExit(current, currentGame))
            {
                return(PgnParseResult.None);
            }

            return(DoParse(current, next, currentGame));
        }
Esempio n. 5
0
        private void Terminate(string result, PgnGame currentGame)
        {
            CheckToFinishMove();

            if (!string.IsNullOrWhiteSpace(_currentMove.Move))
            {
                currentGame.AddMove(_currentMove);
            }

            currentGame.Result = result;

            _stateBuffer.Clear();
            _currentMove = new PgnMove();
        }
Esempio n. 6
0
        /// <summary>
        /// Reads one game from the pgn file.
        /// </summary>
        /// <returns><c>true</c>, if there are more records, otherwise <c>false</c>.</returns>
        public bool ReadGame()
        {
            CheckIfDisposed();

            try
            {
                CurrentGame = InternalReadGame();
            }
            catch (Exception ex)
            {
                throw new PgnReaderException(string.Format("A parsing error occurred."), ex);
            }

            return(CurrentGame != null);
        }
Esempio n. 7
0
        protected bool TryExit(char current, PgnGame game)
        {
            foreach (var item in _exits)
            {
                if (item.Item1(current))
                {
                    OnExit();
                    _history.Pop();
                    _statemachine.SetState(item.Item2());
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 8
0
        protected bool TryTransite(char current, PgnGame game)
        {
            foreach (var item in _transitions)
            {
                if (item.Item1(current))
                {
                    ChangeState(item.Item2(), _currentMove);
                    _history.Push(item.Item2());
                    if (item.Item3 != null)
                    {
                        item.Item3(game);
                    }
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 9
0
        private PgnGame InternalReadGame()
        {
            var currentGame    = new PgnGame();
            var currentCharPos = 0;

            while (true)
            {
                currentCharPos++;

                if (_readerBufferPosition == _charsRead)
                {
                    if (!TryReadNextBlock())
                    {
                        _statemachine.Parse('\0', '\0', currentGame);

                        if (currentGame.Moves.Count == 0)
                        {
                            return(null);
                        }

                        _games.Add(currentGame);
                        return(currentGame);
                    }
                }

                if (_currentChar == null)
                {
                    _currentChar = _readerBuffer[_readerBufferPosition];
                    _readerBufferPosition++;
                }

                _nextChar = _readerBuffer[_readerBufferPosition];
                _readerBufferPosition++;

                if (_statemachine.Parse(_currentChar.Value, _nextChar, currentGame) == PgnParseResult.EndOfGame)
                {
                    _games.Add(currentGame);
                    _currentChar = _nextChar;
                    return(currentGame);
                }

                _currentChar = _nextChar;
            }
        }
Esempio n. 10
0
        protected override PgnParseResult DoParse(char current, char next, PgnGame currentGame)
        {
            if (char.IsWhiteSpace(current) && char.IsWhiteSpace(next))
            {
                //remove double whitespaces
            }
            else if (char.IsWhiteSpace(current))
            {
                CheckToFinishMove();
            }
            else if (current == '\r')
            {
                //remove linebreaks
            }
            else if (current == '-' ||
                     current == '+' ||
                     current == '#')
            {
                _stateBuffer.Add(current);
            }
            else if (char.IsLetterOrDigit(current))
            {
                if (!string.IsNullOrWhiteSpace(_currentMove.Move))
                {
                    _stateBuffer.Clear();

                    _currentMove = new PgnMove();
                }

                _stateBuffer.Add(current);
            }
            else if (current == PgnToken.Period.Token)
            {
                _stateBuffer.Clear();
            }
            else
            {
                _stateBuffer.Add(current);
                //throw new PgnReaderException("RecursiveVariationState: no idea how to handle: " + current);
            }

            return(PgnParseResult.None);
        }
Esempio n. 11
0
 protected abstract PgnParseResult DoParse(char current, char next, PgnGame currentGame);
Esempio n. 12
0
        protected override PgnParseResult DoParse(char current, char next, PgnGame currentGame)
        {
            if (_skipCharsTilEofGame > 0)
            {
                --_skipCharsTilEofGame;
                return(_skipCharsTilEofGame == 0 ? PgnParseResult.EndOfGame : PgnParseResult.None);
            }

            if (current == '1' && next == '/')
            {
                Terminate("1/2-1/2" + "", currentGame);

                _skipCharsTilEofGame = 6;

                return(PgnParseResult.None);
            }
            else if (current == '1' && next == '-')
            {
                Terminate("1-0", currentGame);

                _skipCharsTilEofGame = 2;

                return(PgnParseResult.None);
            }
            else if (current == '0' && next == '-')
            {
                Terminate("0-1", currentGame);

                _skipCharsTilEofGame = 2;

                return(PgnParseResult.None);
            }
            else if (char.IsLetterOrDigit(current))
            {
                if (!string.IsNullOrWhiteSpace(_currentMove.Move))
                {
                    currentGame.AddMove(_currentMove);
                    _stateBuffer.Clear();

                    _currentMove = new PgnMove();
                }

                _stateBuffer.Add(current);
            }
            else if (current == PgnToken.Period.Token)
            {
                _stateBuffer.Clear();
            }
            else if (current == PgnToken.Asterisk.Token)
            {
                Terminate(current + "", currentGame);

                return(PgnParseResult.EndOfGame);
            }
            else if (current == PgnToken.TagBegin.Token || current == '\0')
            {
                string gameResult  = string.Empty;
                string termination = new string(_stateBuffer.ToArray()).Trim();
                foreach (var result in PgnReader.Results)
                {
                    if (termination.EndsWith(result))
                    {
                        gameResult = result;
                    }
                }

                if (string.IsNullOrEmpty(gameResult))
                {
                    CheckToFinishMove();
                    Terminate("*", currentGame);
                }
                else
                {
                    Terminate(gameResult, currentGame);
                }

                return(PgnParseResult.EndOfGame);
            }
            else if (char.IsWhiteSpace(current) && char.IsWhiteSpace(next))
            {
                //remove double whitespaces
            }
            else if (char.IsWhiteSpace(current))
            {
                CheckToFinishMove();

                if (next == PgnToken.TagBegin.Token)
                {
                    if (!string.IsNullOrWhiteSpace(_currentMove.Move))
                    {
                        currentGame.AddMove(_currentMove);
                    }
                    return(PgnParseResult.EndOfGame);
                }
            }
            else if (current == '\r')
            {
                //remove linebreaks
            }
            else if (current == '-' ||
                     current == '+' ||
                     current == '#')
            {
                _stateBuffer.Add(current);
            }
            else
            {
                _stateBuffer.Add(current);
                //throw new PgnReaderException("MovesSectionState: no idea how to handle: " + current);
            }

            return(PgnParseResult.None);
        }
Esempio n. 13
0
 internal void InitGame(PgnGame game)
 {
     _stateBuffer.Clear();
     _currentMove = new PgnMove();
 }