static void Main(string[] args) { using (TextReader input = File.OpenText(args[0])) { int indent = 0; var parser = new Parser(input); while(parser.MoveNext()) { if (parser.Current is StreamEnd || parser.Current is DocumentEnd || parser.Current is SequenceEnd || parser.Current is SequenceEnd || parser.Current is MappingEnd) { --indent; } for(int i = 0; i < indent; ++i) { Console.Write(" "); } Console.WriteLine(parser.Current.ToString()); if (parser.Current is StreamStart || parser.Current is DocumentStart || parser.Current is SequenceStart || parser.Current is MappingStart) { ++indent; } } } }
/// <summary> /// Parse an entire YAML document as an instance of the given type. /// Note that this method is not suitable to parse parts of a YAML document /// because it throws an exception if the end of the stream has not been reached after parsing an instance of the given type. /// </summary> /// <typeparam name="T">The type to parse the YAML document as.</typeparam> /// <param name="reader">A reader over the YAML data.</param> /// <returns>The parsed instance.</returns> public static T ParseDocument <T>(TextReader reader) { var parser = new SharpYaml.Parser(reader); SkipStreamStart(parser); var result = ObjectFromYaml <T>(parser); CheckStreamEnd(parser); return(result); }
private static object FromYaml(string yamlText, bool expectOnlyFrontMatter, out TextPosition position) { position = new TextPosition(); if (yamlText == null) { return(null); } var parser = new Parser(new StringReader(yamlText)); var reader = new EventReader(parser); if (!reader.Accept <StreamStart>()) { return(null); } reader.Expect <StreamStart>(); var docStart = reader.Expect <DocumentStart>(); var hasDocumentStart = true; object result = null; ScriptArray objects = null; // If we expect to read multiple documents, we will return an array of result if (expectOnlyFrontMatter && docStart.IsImplicit) { return(null); } Mark endPosition; while (true) { if (reader.Accept <StreamEnd>()) { var evt = reader.Expect <StreamEnd>(); endPosition = evt.End; break; } if (hasDocumentStart && reader.Accept <DocumentEnd>()) { reader.Expect <DocumentEnd>(); hasDocumentStart = false; if (expectOnlyFrontMatter) { reader.Accept <DocumentStart>(); var nextDocStart = reader.Expect <DocumentStart>(); endPosition = nextDocStart.End; break; } continue; } if (reader.Accept <DocumentStart>()) { reader.Expect <DocumentStart>(); hasDocumentStart = true; } var obj = ReadEvent(reader); if (result == null) { result = obj; } else { if (objects == null) { objects = new ScriptArray { result }; result = objects; } objects.Add(obj); } } position = new TextPosition(endPosition.Index, endPosition.Line, endPosition.Column); return(result); }
private static object FromYaml(string yamlText, string yamlFile, bool expectOnlyFrontMatter, out TextPosition position) { try { position = new TextPosition(); if (yamlText == null) { return(null); } var parser = new Parser(new StringReader(yamlText)); var reader = new EventReader(parser); if (!reader.Accept <StreamStart>()) { return(null); } reader.Expect <StreamStart>(); var docStart = reader.Expect <DocumentStart>(); var hasDocumentStart = true; object result = null; ScriptArray objects = null; // If we expect to read multiple documents, we will return an array of result if (expectOnlyFrontMatter && docStart.IsImplicit) { return(null); } Mark endPosition; while (true) { if (reader.Accept <StreamEnd>()) { var evt = reader.Expect <StreamEnd>(); endPosition = evt.End; break; } if (hasDocumentStart && reader.Accept <DocumentEnd>()) { reader.Expect <DocumentEnd>(); hasDocumentStart = false; if (expectOnlyFrontMatter) { reader.Accept <DocumentStart>(); // Don't consume the token as the parser will try to parse // the following characters and could hit non YAML syntax (in Markdown) // and would throw a parser exception var nextDocStart = reader.Peek <DocumentStart>(); endPosition = nextDocStart.End; break; } continue; } if (reader.Accept <DocumentStart>()) { reader.Expect <DocumentStart>(); hasDocumentStart = true; } var obj = ReadEvent(reader); if (result == null) { result = obj; } else { if (objects == null) { objects = new ScriptArray { result }; result = objects; } objects.Add(obj); } } position = new TextPosition(endPosition.Index, endPosition.Line, endPosition.Column); return(result); } catch (Exception ex) { throw new LunetException($"Error while parsing {yamlFile}. {ex.Message}"); } }