public QuantifierStackFrame(StackFrame parent, QuantifierPattern quant) : this(parent, new RepeaterConsList <BasePattern>(quant.ChildPattern, quant.MaxOccurrences), quant.IsGreedy, -1) { if (quant.MinOccurrences != 0) { quant.AssertCanonicalForm(); } }
private BasePattern reduceQuantifier(QuantifierPattern quant) { if (quant.MaxOccurrences == 0) return GroupPattern.Empty; else if (quant.MinOccurrences == 1 && quant.MaxOccurrences == 1) return quant.ChildPattern; else return quant; }
private BasePattern reduceQuantifier(QuantifierPattern quant) { if (quant.MaxOccurrences == 0) { return(GroupPattern.Empty); } else if (quant.MinOccurrences == 1 && quant.MaxOccurrences == 1) { return(quant.ChildPattern); } else { return(quant); } }
private Parser <char, string> createParser(BasePattern pattern) { if (pattern == null) { throw new ArgumentNullException("pattern.", "Pattern is null when creating match parser."); } switch (pattern.Type) { case PatternType.Group: return(from vs in CharParsers.Sequence(((GroupPattern)pattern).Patterns .Select(p => createParser(p))) select vs.JoinStrings()); case PatternType.Quantifier: QuantifierPattern quant = (QuantifierPattern)pattern; return(from vs in CharParsers.Count(quant.MinOccurrences, quant.MaxOccurrences, createParser(quant.ChildPattern)) select vs.JoinStrings()); case PatternType.Alternation: return(CharParsers.Choice(((AlternationPattern)pattern).Alternatives .Select(p => createParser(p)) .ToArray())); case PatternType.String: return(CharParsers.String(((StringPattern)pattern).Value)); case PatternType.Char: return(from c in CharParsers.Satisfy(((CharPattern)pattern).IsMatch) select new string(c, 1)); default: throw new ApplicationException( string.Format("ExplicitDFAMatcher: unrecognized pattern type ({0}).", pattern.GetType().Name)); } }
public override BasePattern Transform(BasePattern pattern) { if (pattern.Type == PatternType.Quantifier) { QuantifierPattern quant = (QuantifierPattern)pattern; BasePattern transformedChild = Transform(quant.ChildPattern); if (IsEmpty(transformedChild)) { return(GroupPattern.Empty); } BasePattern[] newPatterns = new[] { new QuantifierPattern(transformedChild, quant.MinOccurrences, quant.MinOccurrences, quant.IsGreedy), new QuantifierPattern(transformedChild, 0, quant.MaxOccurrences != null ? quant.MaxOccurrences - quant.MinOccurrences : null, quant.IsGreedy), } .Select(q => reduceQuantifier(q)) .Where(IsNotEmpty) .ToArray(); if (newPatterns.Length == 1) { return(newPatterns[0]); } else { return(new GroupPattern(false, newPatterns)); } } else { return(base.Transform(pattern)); } }
public virtual BasePattern Transform(BasePattern pattern) { // The Identity transform switch (pattern.Type) { case PatternType.Group: GroupPattern group = (GroupPattern)pattern; BasePattern[] newChildren = group.Patterns .Select(p => Transform(p)) .Where(IsNotEmpty) .ToArray(); return(CreateGroupOrSingleton(group.IsCapturing, newChildren)); case PatternType.Quantifier: QuantifierPattern quant = (QuantifierPattern)pattern; BasePattern newChild = Transform(quant.ChildPattern); if (IsNotEmpty(newChild)) { return(new QuantifierPattern(newChild, quant.MinOccurrences, quant.MaxOccurrences, quant.IsGreedy)); } else { return(GroupPattern.Empty); } case PatternType.Alternation: return(new AlternationPattern(((AlternationPattern)pattern).Alternatives .Select(a => Transform(a)))); default: return(pattern); } }
public QuantifierStackFrame(StackFrame parent, QuantifierPattern quant) : this(parent, new RepeaterConsList<BasePattern>(quant.ChildPattern, quant.MaxOccurrences), quant.IsGreedy, -1) { if (quant.MinOccurrences != 0) quant.AssertCanonicalForm(); }