Ejemplo n.º 1
0
        public void AndExtension_Parse_Success_Success()
        {
            // Same as above, just different syntax
            var parser = _anyParser.And(_anyParser);

            var input  = new StringCharacterSequence("abc");
            var result = parser.Parse(input);

            result.Success.Should().BeTrue();
            result.Consumed.Should().Be(2);
        }
Ejemplo n.º 2
0
 public static IParser <List <T> > RepeatedSeparatedBy <T, TS>(this IParser <T> first, IParser <TS> sep)
 {
     return(first.And(sep.IgnoreAnd(RepeatToLinkedList(first))).Map(results =>
     {
         var(head, tail) = results;
         var list = new LinkedList <T> {
             Head = head, Tail = tail
         };
         return ConvertLinkedListToList(list);
     }));
 }
Ejemplo n.º 3
0
 public static IParser <List <T> > Repeat1 <T>(this IParser <T> first)
 {
     return(first.And(RepeatToLinkedList(first)).Map(results =>
     {
         var(head, tail) = results;
         var list = new LinkedList <T> {
             Head = head, Tail = tail
         };
         return ConvertLinkedListToList(list);
     }));
 }
Ejemplo n.º 4
0
 public static IParser <OutputType> AndKeepLeft <OutputType, OtherOutputType>
     (this IParser <OutputType> parser, Lazy <IParser <OtherOutputType> > other) =>
 parser.And(other).Map(leftAndRight => leftAndRight.Left);
        protected XmlParser(bool caseSensitive)
        {
            comparer = caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;

            id = Character(char.IsLetter)
                 .And(
                Character(char.IsLetterOrDigit).NoneOrMore())
                 .Join();

            const char tagStartChar = '<';
            const char tagEndChar   = '>';

            tagStart = Character(tagStartChar);
            tagEnd   = Character(tagEndChar);

            tagName = id;

            openTag = (namePredicate, attributes) =>
                      from _ in tagStart
                      from name in tagName.Where(namePredicate)
                      from attrs in attributes
                      from __ in InsignificantWhiteSpace
                      from empty in Character('/').Maybe()
                      from ___ in tagEnd
                      select Tuple.Create(name, attrs, empty.Any());

            attributeDelimiter = Character('\'').Or(Character('"'));

            attributeName = id;

            attributeValue = from delimeter in attributeDelimiter
                             from value in AnyCharacterUntil(delimeter)
                             from _ in Character(delimeter)
                             select value;

            attribute = predicate => from _ in InsignificantWhiteSpace
                        from name in attributeName.Where(predicate)
                        from __ in InsignificantWhiteSpace
                        from ___ in Character('=')
                        from ____ in InsignificantWhiteSpace
                        from value in attributeValue
                        select new XAttribute(name, value);

            closeTag = from _ in tagStart.And(Character('/'))
                       from name in tagName
                       from __ in InsignificantWhiteSpace
                       from ___ in tagEnd
                       select name;

            text = AnyCharacterUntil(tagStartChar).Select(t => new XText(t));

            comment = from _ in Word(tagStartChar + "!--")
                      from value in AnyCharacterUntil("--")
                      from __ in Word("--" + tagEndChar)
                      select new XComment(value);

            string startCData = tagStartChar + "![CDATA[";
            string endCData   = "]]" + tagEndChar;

            cData = from _ in Word(startCData)
                    from value in AnyCharacterUntil(endCData)
                    from __ in Word(endCData)
                    select new XCData(value);

            element = (namePredicate, attributes, elementContent) =>
                      from _ in InsignificantWhiteSpace
                      let open = openTag(namePredicate, attributes)
                                 from tag in open
                                 from children in tag.Item3
          ? open.Success(Enumerable.Empty <XObject>())  // tag.Item3 indicates an empty XML element
          : elementContent.IgnoreTrailing(
                InsignificantWhiteSpace.IgnoreBefore(
                    closeTag))
#if !SILVERLIGHT || WINDOWS_PHONE
                                 select new XElement(tag.Item1, tag.Item2.Concat(children).ToArray());
#else
                                 select new XElement(tag.Item1, tag.Item2.Cast <XObject>().Concat(children).ToArray());
#endif
        }