/// <summary> /// Internal use only. /// Construct a matcher for a pre-compiled regular expression from program /// (bytecode) data. Internal use only /// </summary> /// <param name="program">Compiled regular expression program</param> internal Regex(RegexProgram program) : this(program, RegexOptions.None) { }
/// <summary> /// Compiles a regular expression pattern into a program runnable by the pattern matcher class 'RE'. /// </summary> /// <param name="pattern">Regular expression pattern to compile</param> /// <returns></returns> internal RegexProgram Compile(String pattern) { //Check if the pattersn is already compiled if (Regex.CacheSize > 0 && Regex.Cache.Contains(pattern)) { foreach (RegexProgram regexProgram in Regex.Cache.ToArray()) if (regexProgram.Pattern.Equals(pattern)) return regexProgram; throw new Exception("Cache Error RegexCompiler.cs Compile Method"); } else { //Maintain CacheSize Requirement if (Regex.CacheSize > 0 && Regex.Cache.Count >= Regex.CacheSize) Regex.Cache.Pop(); // Initialize variables for compilation this.pattern = pattern; // Save pattern in instance variable len = pattern.Length; // Precompute pattern Length for speed idx = 0; // Set parsing index to the first character lenInstruction = 0; // Set emitted instruction count to zero parens = 1; // Set paren level to 1 (the implicit outer parens) // Initialize pass by reference flags value int[] flags = { NODE_TOPLEVEL }; // Parse expression Expression(flags); // Should be at end of input if (idx != len) { if (pattern[idx] == OpCode.Close) SyntaxError("Unmatched close paren"); SyntaxError("Unexpected input remains"); } //Compile the result char[] ins = new char[lenInstruction]; System.Array.Copy(instruction, 0, ins, 0, lenInstruction); //ins.CopyTo(instruction, 0); RegexProgram result = new RegexProgram(parens, ins, pattern); //If caching is enabled add the pattern to the cache //Should probably check for the Compiled Flag before putting it in if (Regex.CacheSize > 0 && !Regex.Cache.Contains(pattern)) { Regex.Cache.Push(result); } return result; } }
/// <summary> /// Internal use only. /// Construct a matcher for a pre-compiled regular expression from program /// bytecode) data. Permits special flags to be passed in to modify matching /// behaviour. /// RegexOptions.Normal // Normal (case-sensitive) matching /// RegexOptions.CaseIndependant // Case folded comparisons /// RegexOptions.Multiline // Newline matches as BOL/EOL /// </summary> /// <param name="program">Compiled regular expression program (see RECompiler)</param> /// <param name="matchFlags">One or more of the MatchOptions</param> internal Regex(RegexProgram program, RegexOptions matchFlags) { Program = program; Options = matchFlags; }