public Prison Load(Stream stream) { var reader = new StreamReader(stream, Encoding.ASCII); var nodes = new Stack <Node>(); Node currentNode = new Prison(); int lineNum = 0; string line; while ((line = reader.ReadLine()) != null) { lineNum++; Tokenize(line); // skip blank lines if (tokens.Count == 0) { continue; } // start a new node if ("BEGIN".Equals(tokens[0])) { nodes.Push(currentNode); string label = tokens[1]; currentNode = currentNode.CreateNode(label); if (tokens.Count > 2) { // inline node if (tokens.Count % 2 != 1) { throw new FormatException( "Unexpected number of tokens in an inline node definition on line " + lineNum); } if (!"END".Equals(tokens[tokens.Count - 1])) { throw new FormatException("Unexpected end of inline node definition on line " + lineNum); } for (int i = 2; i < tokens.Count - 1; i += 2) { string key = tokens[i]; string value = tokens[i + 1]; currentNode.ReadKey(key, value); } Node upperNode = nodes.Pop(); upperNode.FinishedReadingNode(currentNode); currentNode = upperNode; } else { currentNode.DoNotInline = true; } } else if ("END".Equals(tokens[0])) { // end of multi-line section Node upperNode = nodes.Pop(); upperNode.FinishedReadingNode(currentNode); currentNode = upperNode; } else { // inside a multi-line section string key = tokens[0]; string value = tokens[1]; currentNode.ReadKey(key, value); } } if (nodes.Count != 0) { throw new FormatException("Unexpected end of file!"); } return((Prison)currentNode); }