Пример #1
0
        void OptimizeSingleItemParsers(IEnumerable <Parser> children)
        {
            var sequences = from r in children.OfType <SequenceParser>()
                            where
                            r.Items.Count == 1 &&
                            r.GetType() == typeof(SequenceParser)
                            select r;
            var alternations = from r in children.OfType <AlternativeParser>()
                               where
                               r.Items.Count == 1 &&
                               r.GetType() == typeof(AlternativeParser)
                               select r;

            var parsers = sequences.Cast <ListParser>().Union(alternations.Cast <ListParser>()).ToList();

            foreach (var parser in parsers)
            {
                var replacement = parser.Items[0];
                if (parser.Name != null)
                {
                    replacement = new UnaryParser {
                        Inner = replacement, Name = parser.Name
                    };
                }
                Replace(new ParserReplaceArgs(parser, replacement));
            }
        }
Пример #2
0
        public static Parser Named(this Parser parser, string name)
        {
            var unary = new UnaryParser(parser);

            unary.Name = name ?? Guid.NewGuid().ToString();
            return(unary);
        }
Пример #3
0
        void OptimizeSingleItemParsers()
        {
            var parsers = from r in Children.OfType <ListParser>()
                          where
                          r.Items.Count == 1 &&
                          (r is SequenceParser || r is AlternativeParser)
                          select r;

            foreach (var parser in parsers.ToList())
            {
                var replacement = parser.Items[0];
                if (parser.Name != null)
                {
                    replacement = new UnaryParser {
                        Inner = replacement, Name = parser.Name
                    };
                }
                Replace(new ParserReplaceArgs(parser, replacement));
            }
        }
Пример #4
0
 protected UnaryParser(UnaryParser other, ParserCloneArgs args)
     : base(other, args)
 {
     Inner = args.Clone(other.Inner);
 }
Пример #5
0
		void OptimizeSingleItemParsers(IEnumerable<Parser> children)
		{
			var sequences = from r in children.OfType<SequenceParser>()
				where
				r.Items.Count == 1
				&& r.GetType() == typeof(SequenceParser)
				select r;
			var alternations = from r in children.OfType<AlternativeParser>()
				where
				r.Items.Count == 1
				&& r.GetType() == typeof(AlternativeParser)
				select r;

			var parsers = sequences.Cast<ListParser>().Union(alternations.Cast<ListParser>()).ToList();
			foreach (var parser in parsers)
			{
				var replacement = parser.Items[0];
				if (parser.Name != null)
				{
					replacement = new UnaryParser { Inner = replacement, Name = parser.Name };
				}
				Replace(new ParserReplaceArgs(parser, replacement));
			}
		}
Пример #6
0
 protected UnaryParser(UnaryParser other, ParserCloneArgs args)
     : base(other, args)
 {
     Inner = args.Clone(other.Inner);
 }