Compile() static private method

static private Compile ( RegexCode code, RegexOptions options ) : RegexRunnerFactory
code RegexCode
options RegexOptions
return RegexRunnerFactory
Exemplo n.º 1
0
        /// <summary>
        /// Main application entrypoint.
        /// Might make this have methods and be a class rathern the a program...
        /// Then the class can Serialise and Deserialiaze the Regexps
        /// </summary>
        /// <param name="arg">Command line arguments</param>
        static public void Main(String[] arg)
        {
            // Create a compiler object
            RegexCompiler r = new RegexCompiler();

            // Print usage if arguments are incorrect
            if (arg.Length <= 0 || arg.Length % 2 != 0)
            {
                Debug.Print("Usage: recompile <patternname> <pattern>");
                return;
            }

            // Loop through arguments, compiling each
            for (int i = 0, end = arg.Length; i < end; i += 2)
            {
                try
                {
                    // Compile regular expression
                    String name = arg[i];
                    String pattern = arg[i + 1];
                    String instructions = name + "Instructions";

                    // Output program as a nice, formatted char array
                    Debug.Print("\n    // Pre-compiled regular expression '" + pattern + "'\n"
                                     + "    private static  char[] " + instructions + " = \n    {");

                    // Compile program for pattern
                    RegexProgram program = r.Compile(pattern);

                    // Number of columns in output
                    int numColumns = 7;

                    // Loop through program
                    char[] p = program.Instructions;
                    for (int j = 0; j < p.Length; j++)
                    {
                        // End of column?
                        if ((j % numColumns) == 0) Debug.Print("\n        ");

                        // Print char as padded hex number                    
                        String hex = (0).ToHexString();
                        
                        while (hex.Length < 4) hex = "0" + hex;
                        
                        Debug.Print("0x" + hex + ", ");
                    }

                    // End of program block
                    Debug.Print("\n    };");
                    Debug.Print("\n    private static  REProgram " + name + " = new REProgram(" + instructions + ");");
                }
                catch (RegexpSyntaxException e)
                {
                    Debug.Print("Syntax error in expression \"" + arg[i] + "\": " + e.ToString());
                }
                catch (Exception e)
                {
                    Debug.Print("Unexpected exception: " + e.ToString());
                }
            }
        }
Exemplo n.º 2
0
        protected static void PerformTest(string input, string pattern, RegexOptions options)
        {
            var compiler = new RegexCompiler(
                (options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase,
                (options & RegexOptions.Singleline) == RegexOptions.Singleline,
                (options & RegexOptions.Multiline) == RegexOptions.Multiline);
            var engine = compiler.Compile(pattern).Parse(input);

            var matchesEnumerator = engine.GetMatches().GetEnumerator();
            var netMatchesEnumerator = System.Text.RegularExpressions.Regex.Matches(input, pattern, options)
                .Cast<System.Text.RegularExpressions.Match>().GetEnumerator();

            while (true)
            {
                var matchesHasNext = matchesEnumerator.MoveNext();
                var netMatchesHasNext = netMatchesEnumerator.MoveNext();

                var match = matchesEnumerator.Current;
                var netMatch = netMatchesEnumerator.Current;

                object un = match == null ? "un" : "";
                Debug.WriteLine("Match was {0}successful.", un);
                Debug.WriteLine("");
                DumpParseSteps(engine, pattern);

                Assert.That(matchesHasNext, Is.EqualTo(netMatchesHasNext));

                if (!matchesHasNext || !netMatchesHasNext)
                {
                    break;
                }

                Assert.That(match != null, Is.EqualTo(netMatch.Success));

                if (match != null)
                {
                    Assert.That(match.Value, Is.EqualTo(netMatch.Value));
                    Assert.That(match.Groups.Count, Is.EqualTo(netMatch.Groups.Count));

                    foreach (var @group in match.Groups)
                    {
                        var netGroup = netMatch.Groups[@group.Number];
                        Assert.That(@group.Value, Is.EqualTo(netGroup.Value));
                        Assert.That(@group.Captures.Count, Is.EqualTo(netGroup.Captures.Count), "Unequal Capture count in Group");

                        for (int i = 0; i < @group.Captures.Count; i++)
                        {
                            Assert.That(@group.Captures[i].Value, Is.EqualTo(netGroup.Captures[i].Value));
                        }
                    }
                }
            }
        }