public virtual PatternList Parse(string str) { _pat = new Pattern(); _strMatch = new StringMatch(); Anchor endAnchor = null; if (string.IsNullOrEmpty(str)) { return(new PatternList()); } if (str[0] == this.Pattern_Anything) { str = str.TrimStart(this.Pattern_Anything); } else { _pat.Matches.Add(new Anchor() { Type = AnchorType.Start_Absolute }); } if (!string.IsNullOrEmpty(str) && str[str.Length - 1] == this.Pattern_Anything) { str = str.TrimEnd(this.Pattern_Anything); } else { endAnchor = new Anchor() { Type = AnchorType.End_Absolute }; } var i = 0; bool inRange = false; while (i < str.Length) { if (inRange) { if (str[i] == ']') { inRange = false; _pat.Matches.Add(_set); _set = null; } else if (_set.Chars.Count > 0 && str[i] == this.Pattern_SetRange && (i + 1) < str.Length && str[i + 1] != ']') { _set.AddRange((char)(_set.Chars.Last() + 1), str[i + 1]); i++; } else { _set.Chars.Add(str[i]); } } else if (str[i] == this.Pattern_Anything) { FinishStringMatch(); _set = new CharSet('.'); _set.Repeat.MinCount = 0; _set.Repeat.MaxCount = int.MaxValue; _pat.Matches.Add(_set); _set = null; } else if (str[i] == this.Pattern_SingleChar) { FinishStringMatch(); _set = new CharSet('.'); _set.Repeat.MinCount = 1; _set.Repeat.MaxCount = 1; _pat.Matches.Add(_set); _set = null; } else if (str[i] == this.Pattern_SingleDigit) { FinishStringMatch(); _set = new CharSet('d'); _set.Repeat.MinCount = 1; _set.Repeat.MaxCount = 1; _pat.Matches.Add(_set); _set = null; } else if (str[i] == '[' && !inRange && AllowCharSet) { FinishStringMatch(); inRange = true; _set = new CharSet(); if ((i + 1) < str.Length && str[i + 1] == this.Pattern_InverseSet) { _set.InverseSet = true; i++; } } else if (str[i] == this.Pattern_Escape) { i++; _strMatch.Match.Append(str[i]); } else { _strMatch.Match.Append(str[i]); } i++; } if (_strMatch.Match.Length > 0) { _pat.Matches.Add(_strMatch); } if (endAnchor != null) { _pat.Matches.Add(endAnchor); } var patOpts = new PatternList(); patOpts.Patterns.Add(_pat); patOpts.Visit(RegexParser.Simplify); return(patOpts); }
public void Visit(StringMatch value) { // Do Nothing }
public virtual void Visit(StringMatch value) { StartWriting(); if (value.Repeat.MinCount != 1 || value.Repeat.MaxCount != 1) { throw new NotSupportedException(); } char escape = _defn.Pattern_Escape; if (escape == '\0') { escape = EscapeUsed; } var str = value.Match.ToString(); if (_defn.AllowCharSet) { str = str.Replace("[", "[[]") .Replace(_defn.Pattern_Anything.ToString(), "[" + _defn.Pattern_Anything.ToString() + "]"); if (_defn.Pattern_SingleChar != '\0') { str = str.Replace(_defn.Pattern_SingleChar.ToString(), "[" + _defn.Pattern_SingleChar.ToString() + "]"); } if (_defn.Pattern_SingleDigit != '\0') { str = str.Replace(_defn.Pattern_SingleDigit.ToString(), "[" + _defn.Pattern_SingleDigit.ToString() + "]"); } _writer.Write(str); } else { // I need to do escaping if I have an escape character bool needEscape = (escape != '\0'); // If I don't think I need to escape, think again if (!needEscape) { needEscape = (str.IndexOf(_defn.Pattern_Anything) >= 0); needEscape = needEscape || (str.IndexOf(_defn.Pattern_SingleChar) >= 0); needEscape = needEscape || (_defn.Pattern_SingleDigit != '\0' && str.IndexOf(_defn.Pattern_SingleDigit) >= 0); if (needEscape) { EscapeUsed = '`'; escape = '`'; } } if (needEscape) { str = str.Replace(_defn.Pattern_Anything.ToString(), escape + _defn.Pattern_Anything.ToString()). Replace(_defn.Pattern_SingleChar.ToString(), escape + _defn.Pattern_SingleChar.ToString()). Replace(escape.ToString(), escape.ToString() + escape); if (_defn.Pattern_SingleDigit != '\0') { str = str.Replace(_defn.Pattern_SingleDigit.ToString(), escape + _defn.Pattern_SingleDigit.ToString()); } } _writer.Write(str); } }