Parses YAML streams.
Beispiel #1
0
        static void Main(string[] args)
        {
            using (TextReader input = File.OpenText(args[0]))
            {
                int indent = 0;
                Parser 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;
                    }
                }
            }
        }
Beispiel #2
0
        private static async Task Run(CliArgs args)
        {
            if (string.IsNullOrWhiteSpace(args.Target))
            {
                args.Target = Path.ChangeExtension(args.Source, ".jsonl");
            }

            long count = 0;

            using (var reader = new StreamReader(args.Source))
                using (var writer = new StreamWriter(args.Target))
                {
                    var deserializer = new DeserializerBuilder().Build();
                    var parser       = new YamlParser(reader);
                    parser.Expect <StreamStart>();
                    while (parser.Current is DocumentStart)
                    {
                        object source = deserializer.Deserialize(parser);

                        if (source != null)
                        {
                            await writer.WriteLineAsync(JsonConvert.SerializeObject(source));

                            count++;
                        }
                    }
                    parser.Expect <StreamEnd>();
                }

            Console.WriteLine($"Wrote {count} line(s) to {args.Target}");
        }
Beispiel #3
0
        public static IEnumerable <TargetObject> ConvertFromYaml(TargetObject targetObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(targetObject))
            {
                return new TargetObject[] { targetObject }
            }
            ;

            var d = new DeserializerBuilder()
                    .IgnoreUnmatchedProperties()
                    .WithTypeConverter(new PSObjectYamlTypeConverter())
                    .WithNodeTypeResolver(new PSObjectYamlTypeResolver())
                    .Build();

            var reader = ReadAsReader(targetObject.Value, out InputFileInfo sourceInfo);
            var parser = new YamlDotNet.Core.Parser(reader);
            var result = new List <TargetObject>();

            parser.TryConsume <StreamStart>(out _);
            while (parser.Current is DocumentStart)
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                result.AddRange(VisitItem(item, sourceInfo, next));
            }
            return(result.Count == 0 ? EmptyArray : result.ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Throws YamlException
        /// </summary>
        /// <param name="metaModel"></param>
        /// <param name="textReader"></param>
        public void Deserialize(MetaModel.MetaModel metaModel, TextReader textReader)
        {
            Parser p = new Parser(textReader);
            EventReader r = new EventReader(p);

            r.Expect<StreamStart>();
            r.Expect<DocumentStart>();
            r.Expect<MappingStart>();

            Scalar section = r.Peek<Scalar>();
            if (section.Value.Equals(Icons))
            {
                r.Expect<Scalar>();
                DeserializeIcons(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(RecentFiles))
            {
                r.Expect<Scalar>();
                DeserializeRecentFiles(metaModel, r);
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(LastOpenedFile))
            {
                r.Expect<Scalar>();
                metaModel.LastOpenedFile = r.Expect<Scalar>().Value;
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(MapBackColor))
            {
                r.Expect<Scalar>();
                metaModel.MapEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NoteBackColor))
            {
                r.Expect<Scalar>();
                metaModel.NoteEditorBackColor = (Color)(new ColorConverter().ConvertFromString(r.Expect<Scalar>().Value));
                section = r.Peek<Scalar>();
            }

            if (section != null && section.Value.Equals(NodeStyles))
            {
                r.Expect<Scalar>();
                DeserializeNodeStyles(metaModel, r);
                //section = r.Peek<Scalar>(); //uncomment when adding another section
            }

            r.Expect<MappingEnd>();
            r.Expect<DocumentEnd>();
            r.Expect<StreamEnd>();
        }
Beispiel #5
0
        /// <summary>
        /// Parse Stream Reader.
        /// </summary>
        /// <param name="streamReader">Stream reader.</param>
        /// <returns>Deserialized.</returns>
        private T ParseStreamReader(StreamReader streamReader)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(streamReader);

            parser.Expect <StreamStart>();

            while (parser.Accept <DocumentStart>())
            {
                return(deserializer.Deserialize <T>(parser));
            }

            return(null);
        }
Beispiel #6
0
        public static IEnumerable <PSObject> ConvertFromYaml(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

            var d = new DeserializerBuilder()
                    .IgnoreUnmatchedProperties()
                    .WithTypeConverter(new PSObjectYamlTypeConverter())
                    .WithNodeTypeResolver(new PSObjectYamlTypeResolver())
                    .Build();

            var reader = ReadAsReader(sourceObject, out PSSourceInfo source);
            var parser = new YamlDotNet.Core.Parser(reader);

            parser.Expect <StreamStart>();

            var result = new List <PSObject>();

            while (parser.Accept <DocumentStart>())
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                NoteSource(item, source);
                var items = next(item);

                if (items == null)
                {
                    continue;
                }

                result.AddRange(items);
            }

            if (result.Count == 0)
            {
                return(null);
            }

            return(result.ToArray());
        }
        private static T Parse(StreamReader r)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(r);

            // Consume the stream start event "manually"
            parser.Expect <StreamStart>();

            while (parser.Accept <DocumentStart>())
            // Deserialize the document
            {
                return(deserializer.Deserialize <T>(parser));
            }
            return(null);
        }
Beispiel #8
0
        private T ParseStreamReader(StreamReader streamReader)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(streamReader);

            // Consume the stream start event "manually"
            parser.TryConsume <StreamStart>(out _);

            while (parser.TryConsume <DocumentStart>(out _))
            // Deserialize the document
            {
                return(deserializer.Deserialize <T>(parser));
            }

            return(null);
        }
Beispiel #9
0
        /// <summary>
        /// Reads this object's state from a YAML parser.
        /// </summary>
        /// <param name="parser"></param>
        void IYamlSerializable.ReadYaml(Parser parser)
        {
            events.Clear();

            int depth = 0;
            do
            {
                if (!parser.MoveNext())
                {
                    throw new InvalidOperationException("The parser has reached the end before deserialization completed.");
                }

                events.Add(parser.Current);
                depth += parser.Current.NestingIncrease;
            } while (depth > 0);

            Debug.Assert(depth == 0);
        }
		public void Execute(ITextView textView, ITextUndoHistoryRegistry textUndoHistoryRegistry)
		{
			var undoHistory = textUndoHistoryRegistry.RegisterHistory(textView);
			using (var transaction = undoHistory.CreateTransaction("Format Document"))
			{
				var text = textView.TextBuffer.CurrentSnapshot.GetText();

				var formatted = new StringWriter();
				var parser = new Parser(new Scanner(new StringReader(text), skipComments: false));
				var emitter = new Emitter(formatted);

				while (parser.MoveNext())
				{
					emitter.Emit(parser.Current);
				}

				var edit = textView.TextBuffer.CreateEdit();
				edit.Replace(0, text.Length, formatted.ToString());
				edit.Apply();

				transaction.Complete();
			}
		}
Beispiel #11
0
        public T ParseFile(string file)
        {
            var    thisAssembly = Assembly.GetExecutingAssembly();
            string filePath     = "DeviceDetectorNET." + string.Join(".", file.Split('/'));

            using (var stream = thisAssembly.GetManifestResourceStream(filePath))
            {
                using (var r = new StreamReader(stream))
                {
                    var deserializer = new DeserializerBuilder().Build();
                    var parser       = new YamlDotNet.Core.Parser(r);

                    // Consume the stream start event "manually"
                    parser.Expect <StreamStart>();

                    while (parser.Accept <DocumentStart>())
                    // Deserialize the document
                    {
                        return(deserializer.Deserialize <T>(parser));
                    }
                    return(null);
                }
            }
        }
Beispiel #12
0
 private void AssertDoesNotHaveNext(Parser parser)
 {
     Assert.False(parser.MoveNext());
 }
Beispiel #13
0
        private void AssertCurrent(Parser parser, ParsingEvent expected)
        {
            Console.WriteLine(expected.GetType().Name);
            Assert.True(expected.GetType().IsAssignableFrom(parser.Current.GetType()), string.Format("The event is not of the expected type. Exprected: {0}, Actual: {1}", expected.GetType().Name, parser.Current.GetType().Name));

            foreach (var property in expected.GetType().GetProperties()) {
                if(property.PropertyType != typeof(Mark) && property.CanRead) {
                    object value = property.GetValue(parser.Current, null);
                    object expectedValue = property.GetValue(expected, null);
                    if(expectedValue != null && Type.GetTypeCode(expectedValue.GetType()) == TypeCode.Object && expectedValue is IEnumerable) {
                        Console.Write("\t{0} = {{", property.Name);
                        bool isFirst = true;
                        foreach(var item in (IEnumerable)value) {
                            if(isFirst) {
                                isFirst = false;
                            } else {
                                Console.Write(", ");
                            }
                            Console.Write(item);
                        }
                        Console.WriteLine("}");

                        if(expectedValue is ICollection && value is ICollection) {
                            Assert.Equal(((ICollection)expectedValue).Count, ((ICollection)value).Count);
                        }

                        IEnumerator values = ((IEnumerable)value).GetEnumerator();
                        IEnumerator expectedValues = ((IEnumerable)expectedValue).GetEnumerator();
                        while(expectedValues.MoveNext()) {
                            Assert.True(values.MoveNext());
                            Assert.Equal(expectedValues.Current, values.Current);
                        }

                        Assert.False(values.MoveNext());
                    } else {
                        Console.WriteLine("\t{0} = {1}", property.Name, value);
                        Assert.Equal(expectedValue, value);
                    }
                }
            }
        }
 private IList<ParsingEvent> ParsingEventsOf(string text)
 {
     var parser = new Parser(new StringReader(text));
     return EnumerationOf(parser).ToList();
 }
Beispiel #15
0
 private void AssertDoesNotHaveNext(Parser parser)
 {
     Assert.IsFalse(parser.MoveNext(), "The parser should not contain more events.");
 }
Beispiel #16
0
        private void ParseAndEmit(string name)
        {
            string testText = YamlFile(name).ReadToEnd();

            Parser parser = new Parser(new StringReader(testText));
            using(StringWriter output = new StringWriter()) {
                Emitter emitter = new Emitter(output, 2, int.MaxValue, false);
                while(parser.MoveNext()) {
                    //Console.WriteLine(parser.Current.GetType().Name);
                    Console.Error.WriteLine(parser.Current);
                    emitter.Emit(parser.Current);
                }

                string result = output.ToString();

                Console.WriteLine();
                Console.WriteLine("------------------------------");
                Console.WriteLine();
                Console.WriteLine(testText);
                Console.WriteLine();
                Console.WriteLine("------------------------------");
                Console.WriteLine();
                Console.WriteLine(result);
                Console.WriteLine();
                Console.WriteLine("------------------------------");
                Console.WriteLine();

                /*
                Parser resultParser = new Parser(new StringReader(result));
                while(resultParser.MoveNext()) {
                    Console.WriteLine(resultParser.Current.GetType().Name);
                }
                */
                /*

                if(testText != result) {
                    Console.WriteLine();
                    Console.WriteLine("------------------------------");
                    Console.WriteLine();
                    Console.WriteLine("Expected:");
                    Console.WriteLine();
                    Console.WriteLine(testText);
                    Console.WriteLine();
                    Console.WriteLine("------------------------------");
                    Console.WriteLine();
                    Console.WriteLine("Result:");
                    Console.WriteLine();
                    Console.WriteLine(result);
                    Console.WriteLine();
                    Console.WriteLine("------------------------------");
                    Console.WriteLine();
                }

                Assert.AreEqual(testText, result, "The emitter did not generate the correct text.");
                */
            }
        }
Beispiel #17
0
 private void AssertHasNext(Parser parser)
 {
     Assert.True(parser.MoveNext());
 }
Beispiel #18
0
 private void AssertHasNext(Parser parser)
 {
     Assert.IsTrue(parser.MoveNext(), "The parser does not contain more events.");
 }
Beispiel #19
0
 private void AssertNext(Parser parser, ParsingEvent expected)
 {
     AssertHasNext(parser);
     AssertCurrent(parser, expected);
 }
Beispiel #20
0
        public void NewLinesAreParsedAccordingToTheSpecification(string yaml, string expected)
        {
            var parser = new Parser(new StringReader(yaml));

            AssertSequenceOfEventsFrom(Yaml.ParserForText(yaml),
                StreamStart,
                DocumentStart(Implicit),
                LiteralScalar(expected),
                DocumentEnd(Implicit),
                StreamEnd);
        }