public SgfProperty this[string identifier] { get { return(Properties.First(x => x.Identifier == identifier)); } set { SgfProperty property = Properties.FirstOrDefault(x => x.Identifier == identifier); if (property != null) { Properties.Remove(property); } Properties.Add(value); } }
/// <summary> /// Returns a string which represents the object. /// </summary> public override string ToString() { return(this.Identifier + "[" + string.Join("][", this.Values.Select(x => SgfProperty.Escape(x))) + "]"); }
/// <summary> /// Returns the corresponding game tree of a certain range in the given token list. /// </summary> /// <param name="tokens">The token list.</param> /// <param name="start">The start index.</param> /// <param name="end">The end index.</param> public static GameTree Parse(List <Tuple <TokenType, string> > tokens, int start = 0, int end = int.MaxValue) { int i = start; end = Math.Min(end, tokens.Count - 1); GameTree tree = new GameTree(); Node node = new Node(); SgfProperty property = new SgfProperty("", ""); while (i <= end) { Tuple <TokenType, string> token = tokens[i]; if (token.Item1 == TokenType.Semicolon) { node = new Node(); tree.Elements.Add(node); } else if (token.Item1 == TokenType.PropIdent) { property = new SgfProperty(token.Item2, new List <string>()); node.Properties.Add(property); } else if (token.Item1 == TokenType.CValueType) { property.Values.Add(token.Item2); } else if (token.Item1 == TokenType.Parenthesis && token.Item2 == "(") { break; } else if (token.Item1 == TokenType.Parenthesis) { throw new ParseException("Unexpected parenthesis."); } i++; } int depth = 0; int newstart = 0; while (i <= end) { Tuple <TokenType, string> token = tokens[i]; if (token.Item1 == TokenType.Parenthesis && token.Item2 == "(") { depth++; if (depth == 1) { newstart = i + 1; } } else if (token.Item1 == TokenType.Parenthesis && token.Item2 == ")") { depth--; if (depth == 0) { tree.SubTrees.Add(Parse(tokens, newstart, i - 1)); } } i++; } return(tree); }