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)); } }
public static Parser Named(this Parser parser, string name) { var unary = new UnaryParser(parser); unary.Name = name ?? Guid.NewGuid().ToString(); return(unary); }
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)); } }
protected UnaryParser(UnaryParser other, ParserCloneArgs args) : base(other, args) { Inner = args.Clone(other.Inner); }
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)); } }