Example #1
0
        /// <summary>
        /// Parses a SGF property
        /// </summary>
        /// <param name="input">Input</param>
        /// <param name="inputPosition">Current input position</param>
        /// <returns>SGF property</returns>
        private SgfProperty ParseProperty(string input, ref int inputPosition)
        {
            if (!char.IsLetter(input[inputPosition]) || !char.IsUpper(input[inputPosition]))
            {
                throw new SgfParseException($"No valid property starts on input position {inputPosition}");
            }

            //get property name
            StringBuilder identifierBuilder = new StringBuilder();

            while (inputPosition < input.Length && input[inputPosition] != '[')
            {
                identifierBuilder.Append(input[inputPosition]);
                inputPosition++;
            }
            string identifier   = identifierBuilder.ToString();
            var    propertyType = SgfProperty.GetPropertyType(identifier);

            if (propertyType == SgfPropertyType.Invalid)
            {
                throw new SgfParseException($"Invalid SGF property identifier encountered at {inputPosition}");
            }

            //add warning for deprecated property
            if (propertyType == SgfPropertyType.Deprecated)
            {
                _warnings.Add(new SgfParseWarning(SgfParseWarningKind.DeprecatedProperty, inputPosition));
            }

            //add warning for unknown property
            if (propertyType == SgfPropertyType.Unknown)
            {
                _warnings.Add(new SgfParseWarning(SgfParseWarningKind.UnknownProperty, inputPosition));
            }

            SkipInputWhitespace(input, ref inputPosition);

            List <string> values = new List <string>();

            //parse values
            while (inputPosition < input.Length && input[inputPosition] == '[')
            {
                string value = ParseValue(input, ref inputPosition);
                values.Add(value);
                SkipInputWhitespace(input, ref inputPosition);
            }

            //at least one value required
            if (values.Count == 0)
            {
                throw new SgfParseException($"No property values provided for property at {inputPosition}");
            }

            return(SgfProperty.ParseValuesAndCreate(identifier, values.ToArray()));
        }
Example #2
0
 public void PropertyIdentifierWithInvalidCharactersIsInvalid()
 {
     Assert.AreEqual(SgfPropertyType.Invalid, SgfProperty.GetPropertyType(":-"));
 }
Example #3
0
 public void ValidUnknownPropertyWithMoreLettersIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.Unknown, SgfProperty.GetPropertyType("SSSSSS"));
 }
Example #4
0
 public void PropertyIdentifierWithNonCaptialLettersIsInvalid()
 {
     Assert.AreEqual(SgfPropertyType.Invalid, SgfProperty.GetPropertyType("ab"));
 }
Example #5
0
 public void NoTypePropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.NoType, SgfProperty.GetPropertyType("LN"));
 }
Example #6
0
 public void DeprecatedPropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.Deprecated, SgfProperty.GetPropertyType("RG"));
 }
Example #7
0
 public void SetupPropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.Setup, SgfProperty.GetPropertyType("AB"));
 }
Example #8
0
 public void MovePropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.Move, SgfProperty.GetPropertyType("DO"));
 }
Example #9
0
 public void RootPropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.Root, SgfProperty.GetPropertyType("CA"));
 }
Example #10
0
 public void GameInfoPropertyIsRecognized()
 {
     Assert.AreEqual(SgfPropertyType.GameInfo, SgfProperty.GetPropertyType("ON"));
 }
Example #11
0
 public void NullPropertyTypeIsInvalid()
 {
     Assert.AreEqual(SgfPropertyType.Invalid, SgfProperty.GetPropertyType(null));
 }