Exemple #1
0
        public static TextParser <object> NonGreedyContent(params PatternElement[] following)
        {
            if (following.Length == 0)
            {
                return(SpanEx.MatchedBy(Character.AnyChar.Many())
                       .Select(span => span.Length > 0 ? (object)span : null));
            }

            var rest = following[0].Match;

            for (var i = 1; i < following.Length; ++i)
            {
                rest = rest.IgnoreThen(following[i].Match);
            }

            return(i =>
            {
                var remainder = i;
                var attempt = rest(remainder);
                while (!attempt.HasValue || attempt.Remainder == remainder) // A zero-length match doesn't tell us anything
                {
                    remainder = remainder.ConsumeChar().Remainder;
                    attempt = rest(remainder);
                }

                var span = i.Until(remainder);
                return Result.Value(span.Length > 0 ? (object)span : null, i, remainder);
            });
        }
Exemple #2
0
        public async Task SplitsLinesIntoFrames()
        {
            var source = new StringBuilder();

            source.AppendLine("first");
            source.AppendLine("second");

            var reader = new FrameReader(
                new StringReader(source.ToString()),
                SpanEx.MatchedBy(Character.Letter),
                TimeSpan.FromMilliseconds(100));

            var first = await reader.TryReadAsync();

            Assert.True(first.HasValue);
            Assert.Equal("first" + Environment.NewLine, first.Value);

            var second = await reader.TryReadAsync();

            Assert.True(second.HasValue);
            Assert.Equal("second" + Environment.NewLine, second.Value);

            var empty = await reader.TryReadAsync();

            Assert.False(empty.HasValue);
        }
        public PlainTextLogEventReader(TextReader input, string extractionPattern)
        {
            if (extractionPattern == null)
            {
                throw new ArgumentNullException(nameof(extractionPattern));
            }
            _nameValueExtractor = ExtractionPatternInterpreter.CreateNameValueExtractor(ExtractionPatternParser.Parse(extractionPattern));

            _reader = new FrameReader(input, SpanEx.MatchedBy(_nameValueExtractor.StartMarker), TrailingLineArrivalDeadline);
        }
Exemple #4
0
        public async Task TerminatesWhenNoLinesArePresent()
        {
            var reader = new FrameReader(
                new StringReader(""),
                SpanEx.MatchedBy(Character.Letter),
                TimeSpan.FromMilliseconds(100));

            var none = await reader.TryReadAsync();

            Assert.False(none.HasValue);
        }
Exemple #5
0
        public async Task CollectsTrailingLines()
        {
            var source = new StringBuilder();

            source.AppendLine("first");
            source.AppendLine(" some more");
            source.AppendLine(" and more");
            source.AppendLine("second");
            source.AppendLine("third");
            source.AppendLine(" and yet more");

            var frames = await ReadAllFrames(source.ToString(), SpanEx.MatchedBy(Character.Letter));

            Assert.Equal(3, frames.Length);
            Assert.StartsWith("first", frames[0].Value);
            Assert.EndsWith("and more" + Environment.NewLine, frames[0].Value);
        }