<commit_before>﻿/*********************************************************************************
 * PGNSyntax.cs
 * 
 * Copyright (c) 2004-2018 <NAME>
 * 
 *    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.
 * 
 *********************************************************************************/
using System;
using System.Collections.Generic;

namespace Sandra.PGN
{
    public sealed class PGNPly
    {
        public readonly int PlyCount;
        public readonly string Notation;
        public readonly Chess.Variation Variation;

        public PGNPly(int plyCount, string notation, Chess.Variation variation)
        {
            if (notation == null) throw new ArgumentNullException(nameof(notation));
            if (variation == null) throw new ArgumentNullException(nameof(variation));
            PlyCount = plyCount;
            Notation = notation;
            Variation = variation;
        }

        public IEnumerable<PGNTerminalSymbol> GenerateTerminalSymbols(bool precededByFormattedMoveSymbol)
        {
            if (PlyCount % 2 == 0)
            {
                yield return new MoveCounterSymbol(PlyCount / 2 + 1);
            }
            else if (!precededByFormattedMoveSymbol)
            {
                yield return new MoveCounterSymbol(PlyCount / 2 + 1);
                yield return new BlackToMoveEllipsisSymbol();
            }
            yield return new FormattedMoveSymbol(this);
        }
    }

    public interface PGNTerminalSymbol : IEquatable<PGNTerminalSymbol>
    {
        void Accept(PGNTerminalSymbolVisitor visitor);
        TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor);
    }

    public abstract class PGNTerminalSymbolVisitor
    {
        public virtual void DefaultVisit(PGNTerminalSymbol symbol) { }
        public virtual void Visit(PGNTerminalSymbol symbol) { if (symbol != null) symbol.Accept(this); }
        public virtual void VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitMoveCounterSymbol(MoveCounterSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSideLineEndSymbol(SideLineEndSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSideLineStartSymbol(SideLineStartSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSpaceSymbol(SpaceSymbol symbol) => DefaultVisit(symbol);
    }

    public abstract class PGNTerminalSymbolVisitor<TResult>
    {
        public virtual TResult DefaultVisit(PGNTerminalSymbol symbol) => default(TResult);
        public virtual TResult Visit(PGNTerminalSymbol symbol) => symbol == null ? default(TResult) : symbol.Accept(this);
        public virtual TResult VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitMoveCounterSymbol(MoveCounterSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSideLineEndSymbol(SideLineEndSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSideLineStartSymbol(SideLineStartSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSpaceSymbol(SpaceSymbol symbol) => DefaultVisit(symbol);
    }

    public sealed class SpaceSymbol : PGNTerminalSymbol
    {
        public const string SpaceText = " ";

        public bool Equals(PGNTerminalSymbol other) => other is SpaceSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSpaceSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSpaceSymbol(this);
    }

    public sealed class SideLineStartSymbol : PGNTerminalSymbol
    {
        public const string SideLineStartText = "(";

        public bool Equals(PGNTerminalSymbol other) => other is SideLineStartSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSideLineStartSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSideLineStartSymbol(this);
    }

    public sealed class SideLineEndSymbol : PGNTerminalSymbol
    {
        public const string SideLineEndText = ")";

        public bool Equals(PGNTerminalSymbol other) => other is SideLineEndSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSideLineEndSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSideLineEndSymbol(this);
    }

    public sealed class BlackToMoveEllipsisSymbol : PGNTerminalSymbol
    {
        public const string EllipsisText = "..";

        public bool Equals(PGNTerminalSymbol other) => other is BlackToMoveEllipsisSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitBlackToMoveEllipsisSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitBlackToMoveEllipsisSymbol(this);
    }

    public sealed class MoveCounterSymbol : PGNTerminalSymbol
    {
        public readonly int MoveCounter;

        public MoveCounterSymbol(int moveCounter)
        {
            MoveCounter = moveCounter;
        }

        public bool Equals(PGNTerminalSymbol other) => other is MoveCounterSymbol && MoveCounter == ((MoveCounterSymbol)other).MoveCounter;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitMoveCounterSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitMoveCounterSymbol(this);
    }

    public sealed class FormattedMoveSymbol : PGNTerminalSymbol
    {
        public readonly PGNPly Ply;

        public FormattedMoveSymbol(PGNPly ply)
        {
            Ply = ply;
        }

        public bool Equals(PGNTerminalSymbol other) => other is FormattedMoveSymbol && Ply.Variation == ((FormattedMoveSymbol)other).Ply.Variation;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitFormattedMoveSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitFormattedMoveSymbol(this);
    }

    public class PGNTerminalSymbolTextGenerator : PGNTerminalSymbolVisitor<string>
    {
        public override string VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => BlackToMoveEllipsisSymbol.EllipsisText;
        public override string VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => symbol.Ply.Notation;
        public override string VisitMoveCounterSymbol(MoveCounterSymbol symbol) => $"{symbol.MoveCounter}.";<commit_msg>Refer to the PGNPly from MoveCounterSymbol.<commit_after>﻿/*********************************************************************************
 * PGNSyntax.cs
 * 
 * Copyright (c) 2004-2018 <NAME>
 * 
 *    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.
 * 
 *********************************************************************************/
using System;
using System.Collections.Generic;

namespace Sandra.PGN
{
    public sealed class PGNPly
    {
        public readonly int PlyCount;
        public readonly string Notation;
        public readonly Chess.Variation Variation;

        public PGNPly(int plyCount, string notation, Chess.Variation variation)
        {
            if (notation == null) throw new ArgumentNullException(nameof(notation));
            if (variation == null) throw new ArgumentNullException(nameof(variation));
            PlyCount = plyCount;
            Notation = notation;
            Variation = variation;
        }

        public IEnumerable<PGNTerminalSymbol> GenerateTerminalSymbols(bool precededByFormattedMoveSymbol)
        {
            if (PlyCount % 2 == 0)
            {
                yield return new MoveCounterSymbol(this);
            }
            else if (!precededByFormattedMoveSymbol)
            {
                yield return new MoveCounterSymbol(this);
                yield return new BlackToMoveEllipsisSymbol();
            }
            yield return new FormattedMoveSymbol(this);
        }
    }

    public interface PGNTerminalSymbol : IEquatable<PGNTerminalSymbol>
    {
        void Accept(PGNTerminalSymbolVisitor visitor);
        TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor);
    }

    public abstract class PGNTerminalSymbolVisitor
    {
        public virtual void DefaultVisit(PGNTerminalSymbol symbol) { }
        public virtual void Visit(PGNTerminalSymbol symbol) { if (symbol != null) symbol.Accept(this); }
        public virtual void VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitMoveCounterSymbol(MoveCounterSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSideLineEndSymbol(SideLineEndSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSideLineStartSymbol(SideLineStartSymbol symbol) => DefaultVisit(symbol);
        public virtual void VisitSpaceSymbol(SpaceSymbol symbol) => DefaultVisit(symbol);
    }

    public abstract class PGNTerminalSymbolVisitor<TResult>
    {
        public virtual TResult DefaultVisit(PGNTerminalSymbol symbol) => default(TResult);
        public virtual TResult Visit(PGNTerminalSymbol symbol) => symbol == null ? default(TResult) : symbol.Accept(this);
        public virtual TResult VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitMoveCounterSymbol(MoveCounterSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSideLineEndSymbol(SideLineEndSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSideLineStartSymbol(SideLineStartSymbol symbol) => DefaultVisit(symbol);
        public virtual TResult VisitSpaceSymbol(SpaceSymbol symbol) => DefaultVisit(symbol);
    }

    public sealed class SpaceSymbol : PGNTerminalSymbol
    {
        public const string SpaceText = " ";

        public bool Equals(PGNTerminalSymbol other) => other is SpaceSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSpaceSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSpaceSymbol(this);
    }

    public sealed class SideLineStartSymbol : PGNTerminalSymbol
    {
        public const string SideLineStartText = "(";

        public bool Equals(PGNTerminalSymbol other) => other is SideLineStartSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSideLineStartSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSideLineStartSymbol(this);
    }

    public sealed class SideLineEndSymbol : PGNTerminalSymbol
    {
        public const string SideLineEndText = ")";

        public bool Equals(PGNTerminalSymbol other) => other is SideLineEndSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitSideLineEndSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitSideLineEndSymbol(this);
    }

    public sealed class BlackToMoveEllipsisSymbol : PGNTerminalSymbol
    {
        public const string EllipsisText = "..";

        public bool Equals(PGNTerminalSymbol other) => other is BlackToMoveEllipsisSymbol;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitBlackToMoveEllipsisSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitBlackToMoveEllipsisSymbol(this);
    }

    public sealed class MoveCounterSymbol : PGNTerminalSymbol
    {
        public readonly PGNPly Ply;

        public MoveCounterSymbol(PGNPly ply)
        {
            Ply = ply;
        }

        public bool Equals(PGNTerminalSymbol other) => other is MoveCounterSymbol && Ply.Variation == ((MoveCounterSymbol)other).Ply.Variation;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitMoveCounterSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitMoveCounterSymbol(this);
    }

    public sealed class FormattedMoveSymbol : PGNTerminalSymbol
    {
        public readonly PGNPly Ply;

        public FormattedMoveSymbol(PGNPly ply)
        {
            Ply = ply;
        }

        public bool Equals(PGNTerminalSymbol other) => other is FormattedMoveSymbol && Ply.Variation == ((FormattedMoveSymbol)other).Ply.Variation;
        public void Accept(PGNTerminalSymbolVisitor visitor) => visitor.VisitFormattedMoveSymbol(this);
        public TResult Accept<TResult>(PGNTerminalSymbolVisitor<TResult> visitor) => visitor.VisitFormattedMoveSymbol(this);
    }

    public class PGNTerminalSymbolTextGenerator : PGNTerminalSymbolVisitor<string>
    {
        public override string VisitBlackToMoveEllipsisSymbol(BlackToMoveEllipsisSymbol symbol) => BlackToMoveEllipsisSymbol.EllipsisText;
        public override string VisitFormattedMoveSymbol(FormattedMoveSymbol symbol) => symbol.Ply.Notation;
        public override string VisitMoveCounterSymbol(MoveCounterSymbol symbol) => $"{symbol.Ply.PlyCount / 2 + 1}.";
{'fraction_non_alphanumeric': 0.09303904923599321, 'fraction_numerical': 0.002308998302207131, 'mean_word_length': 3.7549241201162413, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '639575', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3630, 'n_words': 948}