Example #1
0
        public static void Main()
        {
            // load the assembly of CodeWorker
            CodeWorker.Main.initialize();

            try
            {
                // compile a BNF script file and execute the parsing
                CodeWorker.ParseTree         theContext   = new CodeWorker.ParseTree();
                CodeWorker.CompiledBNFScript theBNFScript = new CodeWorker.CompiledBNFScript();
                theBNFScript.buildFromFile("../../data/MatchingTest/CSVParser.cwp");
                theBNFScript.parse(theContext, "../../data/MatchingTest/example.csv");

                // display all rules of the grammar
                CodeWorker.ParseTree   rulesNode = theContext.getNode("rules");
                CodeWorker.ParseTree[] allRules  = rulesNode.array;
                System.Console.WriteLine("all rules:");
                for (int i = 0; i < allRules.GetLength(0); ++i)
                {
                    System.Console.WriteLine("\t" + allRules[i].text);
                }

                // display the coverage of the text by the BNF grammar
                CodeWorker.ParseTree   areasNode    = theContext.getNode("areas");
                CodeWorker.ParseTree[] allPositions = areasNode.array;
                System.Console.WriteLine("");
                System.Console.WriteLine("coverage of text by BNF rules:");
                displayCoverage(allPositions);

                // choose default colors for the 8 first rules
                string[] backgroundColors = { "#BBBBBB", "#808080", null, "red", null, null, "orange", null };
                string[] foregroundColors = { null, null, null, "yellow", "magenta", "pink", "green", "blue" };
                System.Console.WriteLine("");
                System.Console.WriteLine("highlighted rules (8 first rules of the grammar):");
                for (int i = 0; i < allRules.GetLength(0); ++i)
                {
                    string backgroundColor = backgroundColors[i];
                    if (i < backgroundColors.GetLength(0))
                    {
                        backgroundColor = backgroundColors[i];
                        if (backgroundColor != null)
                        {
                            allRules[i].insertNode("color").insertNode("background").text = backgroundColor;
                        }
                    }
                    string foregroundColor = foregroundColors[i];
                    if (i < foregroundColors.GetLength(0))
                    {
                        foregroundColor = foregroundColors[i];
                        if (foregroundColor != null)
                        {
                            allRules[i].insertNode("color").insertNode("foreground").text = foregroundColor;
                        }
                    }
                    if ((foregroundColor != null) || (backgroundColor != null))
                    {
                        System.Console.WriteLine("\t" + allRules[i].text + " [" + backgroundColor + ", " + foregroundColor + "]");
                    }
                    else
                    {
                        System.Console.WriteLine("\t" + allRules[i].text + " ... ignored");
                    }
                }

                // generate the syntax highlighting in HTML, according to the BNF.
                // Note: "BNFcoverage.cwt" works for ANY grammar!
                CodeWorker.CompiledTranslationScript theCSV2HTMLScript = new CodeWorker.CompiledTranslationScript();
                theCSV2HTMLScript.buildFromFile("../../data/MatchingTest/BNFcoverage.cwp");
                theCSV2HTMLScript.translate(theContext, "../../data/MatchingTest/example.csv", "../../data/MatchingTest/example.csv.html");

                // to finish, display of the whole parse tree, up to depth = 4
                System.Console.WriteLine("");
                System.Console.WriteLine("display of the whole parse tree, up to depth = 4:");
                CodeWorker.Runtime.traceObject(theContext, 4);
            }
            catch (System.Exception exception)
            {
                System.Console.WriteLine(exception);
            }
            CodeWorker.Main.terminate();
        }
Example #2
0
        public static void Main()
        {
            // load the assembly of CodeWorker
            CodeWorker.Main.initialize();

            try
            {
                // compile a BNF script file and execute the parsing
                CodeWorker.ParseTree theContext = new CodeWorker.ParseTree();
                CodeWorker.CompiledBNFScript theBNFScript = new CodeWorker.CompiledBNFScript();
                theBNFScript.buildFromFile("../../data/MatchingTest/CSVParser.cwp");
                theBNFScript.parse(theContext, "../../data/MatchingTest/example.csv");

                // display all rules of the grammar
                CodeWorker.ParseTree rulesNode = theContext.getNode("rules");
                CodeWorker.ParseTree[] allRules = rulesNode.array;
                System.Console.WriteLine("all rules:");
                for (int i = 0; i < allRules.GetLength(0); ++i)
                {
                    System.Console.WriteLine("\t" + allRules[i].text);
                }

                // display the coverage of the text by the BNF grammar
                CodeWorker.ParseTree areasNode = theContext.getNode("areas");
                CodeWorker.ParseTree[] allPositions = areasNode.array;
                System.Console.WriteLine("");
                System.Console.WriteLine("coverage of text by BNF rules:");
                displayCoverage(allPositions);

                // choose default colors for the 8 first rules
                string[] backgroundColors = {"#BBBBBB", "#808080", null, "red", null, null, "orange", null};
                string[] foregroundColors = {null, null, null, "yellow", "magenta", "pink", "green", "blue"};
                System.Console.WriteLine("");
                System.Console.WriteLine("highlighted rules (8 first rules of the grammar):");
                for (int i = 0; i < allRules.GetLength(0); ++i)
                {
                    string backgroundColor = backgroundColors[i];
                    if (i < backgroundColors.GetLength(0))
                    {
                        backgroundColor = backgroundColors[i];
                        if (backgroundColor != null) allRules[i].insertNode("color").insertNode("background").text = backgroundColor;
                    }
                    string foregroundColor = foregroundColors[i];
                    if (i < foregroundColors.GetLength(0))
                    {
                        foregroundColor = foregroundColors[i];
                        if (foregroundColor != null) allRules[i].insertNode("color").insertNode("foreground").text = foregroundColor;
                    }
                    if ((foregroundColor != null) || (backgroundColor != null))
                    {
                        System.Console.WriteLine("\t" + allRules[i].text + " [" + backgroundColor + ", " + foregroundColor + "]");
                    }
                    else
                    {
                        System.Console.WriteLine("\t" + allRules[i].text + " ... ignored");
                    }
                }

                // generate the syntax highlighting in HTML, according to the BNF.
                // Note: "BNFcoverage.cwt" works for ANY grammar!
                CodeWorker.CompiledTranslationScript theCSV2HTMLScript = new CodeWorker.CompiledTranslationScript();
                theCSV2HTMLScript.buildFromFile("../../data/MatchingTest/BNFcoverage.cwp");
                theCSV2HTMLScript.translate(theContext, "../../data/MatchingTest/example.csv", "../../data/MatchingTest/example.csv.html");

                // to finish, display of the whole parse tree, up to depth = 4
                System.Console.WriteLine("");
                System.Console.WriteLine("display of the whole parse tree, up to depth = 4:");
                CodeWorker.Runtime.traceObject(theContext, 4);
            }
            catch(System.Exception exception)
            {
                System.Console.WriteLine(exception);
            }
            CodeWorker.Main.terminate();
        }