Example #1
0
        /// <summary>
        /// Constructs a parser that consumes a whitespace and all comments
        /// parsed by the commentParser.AnyComment parser, but parses only one trailing
        /// comment that starts exactly on the last line of the parsed value.
        /// </summary>
        /// <typeparam name="T">The result type of the given parser.</typeparam>
        /// <param name="parser">The parser to wrap.</param>
        /// <param name="commentParser">The comment parser.</param>
        /// <returns>An extended Token() version of the given parser.</returns>
        public static Parser <ICommented <T> > Commented <T>(this Parser <T> parser, IComment commentParser = null)
        {
            if (parser == null)
            {
                throw new ArgumentNullException(nameof(parser));
            }

            // consume any comment supported by the comment parser
            var comment = (commentParser ?? DefaultCommentParser).AnyComment;

            // parses any whitespace except for the new lines
            var whiteSpaceExceptForNewLine = WhiteSpace.Except(Chars("\r\n")).Many().Text();

            // returns true if the second span starts on the first span's last line
            bool IsSameLine(ITextSpan <T> first, ITextSpan <string> second) =>
            first.End.Line == second.Start.Line;

            // single comment span followed by a whitespace
            var commentSpan =
                from cs in comment.Span()
                from ws in whiteSpaceExceptForNewLine
                select cs;

            // add leading and trailing comments to the parser
            return
                (from leadingWhiteSpace in WhiteSpace.Many()
                 from leadingComments in comment.Token().Many()
                 from valueSpan in parser.Span()
                 from trailingWhiteSpace in whiteSpaceExceptForNewLine
                 from trailingPreview in commentSpan.Many().Preview()
                 let trailingCount = trailingPreview.GetOrElse(Enumerable.Empty <ITextSpan <string> >())
                                     .Where(c => IsSameLine(valueSpan, c)).Count()
                                     from trailingComments in commentSpan.Repeat(trailingCount)
                                     select new CommentedValue <T>(leadingComments, valueSpan.Value, trailingComments.Select(c => c.Value)));
        }
Example #2
0
 /// <summary>
 /// Succeeds if the specified parser succeeds, ignoring any whitespace characters afterward.
 /// </summary>
 public static IParser <T> TrimEnd <T>(this IParser <T> parser) => parser.FollowedBy(WhiteSpace.Many());
Example #3
0
 /// <summary>
 /// Succeeds if the specified parser succeeds, ignoring any whitespace characters beforehand.
 /// </summary>
 public static IParser <T> TrimStart <T>(this IParser <T> parser) => parser.PrecededBy(WhiteSpace.Many());
Example #4
0
 /// <summary>
 /// Succeeds if the specified parser succeeds, ignoring any whitespace characters beforehand or afterward.
 /// </summary>
 public static IParser <T> Trim <T>(this IParser <T> parser) => parser.Bracketed(WhiteSpace.Many());