/// <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());
                }
            }
        }
示例#2
0
        // Compile regular expressions into an assembly on disk.
        internal static void CompileToAssembly(RegexCompilationInfo[] regexes, AssemblyName an, CustomAttributeBuilder[] attribs, String resourceFile) {
            RegexCompiler c = new RegexCompiler(0, an, attribs, resourceFile);

            for (int i=0; i<regexes.Length; i++) {
                String pattern = regexes[i].Pattern;
                RegexOptions options = regexes[i].Options;
                String fullname = regexes[i].Namespace + "." + regexes[i].Name;

                RegexTree tree = RegexParser.Parse(pattern, options);
                RegexCode code = RegexWriter.Write(tree);

                Type factory;

                new ReflectionPermission(PermissionState.Unrestricted).Assert();
                try {
                    factory = c.FactoryFromCode(code, options, fullname);
                    c.GenerateRegexType(pattern, options, fullname, regexes[i].IsPublic, code, tree, factory);
                }
                finally {
                    CodeAccessPermission.RevertAssert();
                }
            }

            c.Save();
        }
示例#3
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));
                        }
                    }
                }
            }
        }
示例#4
0
        // Gets the unique-for-regex dynamic module for this thread
        internal static RegexCompiler GetThreadCompiler() {
            RegexCompiler compiler = (RegexCompiler)Thread.GetData(_moduleSlot);

            if (compiler == null) {
                int moduleNum;

                lock (_syncObject) {
                    moduleNum = _moduleCount++;
                }

                compiler = new RegexCompiler(moduleNum);
                Thread.SetData(_moduleSlot, compiler);
            }

            return compiler;
        }