Exemplo n.º 1
0
        public void CreateGrammar()
        {
            var grammar = JsonGrammar.Create();

            Assert.AreEqual(17, grammar.Productions.Count);
            Assert.AreEqual(11, grammar.Terminals.Count);
            Assert.AreEqual(7, grammar.Nonterminals.Count);
        }
        public void VisitorTest()
        {
            var jsonGrammar = new JsonGrammar();
            var result = jsonGrammar.Parser.Parse(json).Value;
            var testVisitor = new TestVisitor();

            testVisitor.Visit(result);
            foreach (var property in testVisitor.GetType().GetFields().Where(f => f.FieldType == typeof(bool)))
                Assert.IsTrue((bool)property.GetValue(testVisitor));
        }
Exemplo n.º 3
0
        public bool Parse()
        {
            var grammar     = new JsonGrammar();
            var parseEngine = new ParseEngine(grammar);
            var parseRunner = new ParseRunner(parseEngine, json);

            while (!parseRunner.EndOfStream() && parseRunner.Read())
            {
            }
            return(parseRunner.ParseEngine.IsAccepted());
        }
Exemplo n.º 4
0
        public void VisitorTest()
        {
            var jsonGrammar = new JsonGrammar();
            var result      = jsonGrammar.Parser.Parse(json).Value;
            var testVisitor = new TestVisitor();

            testVisitor.Visit(result);
            foreach (var property in testVisitor.GetType().GetFields().Where(f => f.FieldType == typeof(bool)))
            {
                Assert.IsTrue((bool)property.GetValue(testVisitor));
            }
        }
Exemplo n.º 5
0
 static void ParseFile(string fileName)
 {
     try
     {
         var input  = File.ReadAllText(fileName);
         var parsed = JsonGrammar.ParseInput(input);
         Console.WriteLine(parsed["array"][1]);
         Console.WriteLine(parsed["object"]["fifth"]);
         Console.WriteLine(parsed);
     }
     catch (Exception exception)
     {
         Console.WriteLine($"{fileName} :: {exception}");
     }
 }
Exemplo n.º 6
0
        private static void Main(string[] args)
        {
            var sampleBnf = File.ReadAllText(
                Path.Combine(Environment.CurrentDirectory, "10000.json"));

            var grammar = new JsonGrammar();

            var loopCount = 100;

            for (long i = 0; i < loopCount; i++)
            {
                Console.WriteLine($"Iteration {i} of {loopCount}");
                var parseEngine = new ParseEngine(grammar);
                var parseRunner = new ParseRunner(parseEngine, sampleBnf);

                while (!parseRunner.EndOfStream() && parseRunner.Read())
                {
                }

                var result = parseRunner.ParseEngine.IsAccepted();
            }
        }
Exemplo n.º 7
0
 public static void Initialize(TestContext testContext)
 {
     grammar = new JsonGrammar();
 }
Exemplo n.º 8
0
//        static string path2 = @"..\..\..\Sarcasm.UnitTest\Test files\Binary1.expr";

        static void Main(string[] args)
        {
            var stopwatch = Stopwatch.StartNew();

            stopwatch.Start();
            var grammar = new MiniPLG.GrammarP();
            ShowTimeAndRestart(stopwatch, "Creation of grammar");

            var parser = MultiParser.Create(grammar);
            ShowTimeAndRestart(stopwatch, "Creation of parser");

            var parseTree = parser.Parse(File.ReadAllText(path), path);
            ShowTimeAndRestart(stopwatch, "Parsing");

            var astRootValue = parseTree.RootAstValue;

//            string generatedCode = new MiniPLC.CSharpGenerator().Generate(astRootValue);
//            string generatedCode = new MiniPLC.CppGenerator().Generate(astRootValue);

#if false
            var jsonGrammar = new JsonGrammar();

            Unparser universalUnparser = new Unparser(jsonGrammar);
            var universalParser = MultiParser.Create(jsonGrammar);
            string text = universalUnparser.Unparse(astRootValue).AsText(universalUnparser);

            var foo = universalUnparser.Unparse(astRootValue).Select(utoken => utoken.GetDecoration()).ToList();

            var parseTree2 = universalParser.Parse(text);
            var astRootValue2 = parseTree2.RootAstValue;

            string text2 = universalUnparser.Unparse(astRootValue2).AsText(universalUnparser);

            bool eq = text == text2;
#endif

            Unparser unparser = new Unparser(grammar);
            ShowTimeAndRestart(stopwatch, "Creation of unparser");

            //var utokens = unparser.Unparse(astRootValue).ToList();
            //ShowTimeAndRestart(stopwatch, "Unparsing to utokens");

            //unparser.EnableParallelProcessing = false;
            //string unparsedText = unparser.Unparse(astRootValue).AsText(unparser);
            //var document = parseTree.GetDocument();
            //string unparsedText = unparser.Unparse(document).AsText(unparser);
            //ShowTimeAndRestart(stopwatch, "Converting utokens to string");

            unparser.EnableParallelProcessing = false;
            //foreach (Utoken utoken in unparser.Unparse(astRootValue))
            //    Console.WriteLine(utoken.ToString());
            unparser.Unparse(astRootValue).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Sequential unparsing to string");

            unparser.EnableParallelProcessing = true;
            //foreach (Utoken utoken in unparser.Unparse(astRootValue))
            //    Console.WriteLine(utoken.ToString());
            unparser.Unparse(astRootValue).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Parallel unparsing to string");

            //var utokensReverse = unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ToList();
            //ShowTimeAndRestart(stopwatch, "Reverse unparsing to utokens");

            unparser.EnableParallelProcessing = false;
            unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Reverse sequential unparsing to string");

            unparser.EnableParallelProcessing = true;
            unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Reverse parallel unparsing to string");

            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);
            //stopwatch.Start();
            //var parser2 = MultiParser.Create(grammar);
            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);

            //var parseTree2 = parser.Parse(File.ReadAllText(path2), path2, grammar.B.Expression);
            //var parseTree3 = parser.Parse(File.ReadAllText(path2), path2, (NonTerminal)grammar.B.Expression);
            //MiniPL.DomainDefinitions.Expression astValue2 = parseTree2.RootAstValue;

            //Unparser unparser = new Unparser(grammar);

            //Directory.CreateDirectory("unparse_logs");
            //var stream = File.Create(@"unparse_logs\unparsed_text");
            //unparser.Unparse(parseTree.Root.AstNode).WriteToStream(stream, unparser);

            ////string str = unparser.Unparse(parseTree.Root.AstNode).AsString(unparser);
            ////Console.WriteLine(str);
        }
Exemplo n.º 9
0
//        static string path2 = @"..\..\..\Sarcasm.UnitTest\Test files\Binary1.expr";

        static void Main(string[] args)
        {
            var stopwatch = Stopwatch.StartNew();

            stopwatch.Start();
            var grammar = new MiniPLG.GrammarP();

            ShowTimeAndRestart(stopwatch, "Creation of grammar");

            var parser = MultiParser.Create(grammar);

            ShowTimeAndRestart(stopwatch, "Creation of parser");

            var parseTree = parser.Parse(File.ReadAllText(path), path);

            ShowTimeAndRestart(stopwatch, "Parsing");

            var astRootValue = parseTree.RootAstValue;

//            string generatedCode = new MiniPLC.CSharpGenerator().Generate(astRootValue);
//            string generatedCode = new MiniPLC.CppGenerator().Generate(astRootValue);

#if false
            var jsonGrammar = new JsonGrammar();

            Unparser universalUnparser = new Unparser(jsonGrammar);
            var      universalParser   = MultiParser.Create(jsonGrammar);
            string   text = universalUnparser.Unparse(astRootValue).AsText(universalUnparser);

            var foo = universalUnparser.Unparse(astRootValue).Select(utoken => utoken.GetDecoration()).ToList();

            var parseTree2    = universalParser.Parse(text);
            var astRootValue2 = parseTree2.RootAstValue;

            string text2 = universalUnparser.Unparse(astRootValue2).AsText(universalUnparser);

            bool eq = text == text2;
#endif

            Unparser unparser = new Unparser(grammar);
            ShowTimeAndRestart(stopwatch, "Creation of unparser");

            //var utokens = unparser.Unparse(astRootValue).ToList();
            //ShowTimeAndRestart(stopwatch, "Unparsing to utokens");

            //unparser.EnableParallelProcessing = false;
            //string unparsedText = unparser.Unparse(astRootValue).AsText(unparser);
            //var document = parseTree.GetDocument();
            //string unparsedText = unparser.Unparse(document).AsText(unparser);
            //ShowTimeAndRestart(stopwatch, "Converting utokens to string");

            unparser.EnableParallelProcessing = false;
            //foreach (Utoken utoken in unparser.Unparse(astRootValue))
            //    Console.WriteLine(utoken.ToString());
            unparser.Unparse(astRootValue).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Sequential unparsing to string");

            unparser.EnableParallelProcessing = true;
            //foreach (Utoken utoken in unparser.Unparse(astRootValue))
            //    Console.WriteLine(utoken.ToString());
            unparser.Unparse(astRootValue).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Parallel unparsing to string");

            //var utokensReverse = unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ToList();
            //ShowTimeAndRestart(stopwatch, "Reverse unparsing to utokens");

            unparser.EnableParallelProcessing = false;
            unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Reverse sequential unparsing to string");

            unparser.EnableParallelProcessing = true;
            unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll();
            ShowTimeAndRestart(stopwatch, "Reverse parallel unparsing to string");

            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);
            //stopwatch.Start();
            //var parser2 = MultiParser.Create(grammar);
            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);

            //var parseTree2 = parser.Parse(File.ReadAllText(path2), path2, grammar.B.Expression);
            //var parseTree3 = parser.Parse(File.ReadAllText(path2), path2, (NonTerminal)grammar.B.Expression);
            //MiniPL.DomainDefinitions.Expression astValue2 = parseTree2.RootAstValue;

            //Unparser unparser = new Unparser(grammar);

            //Directory.CreateDirectory("unparse_logs");
            //var stream = File.Create(@"unparse_logs\unparsed_text");
            //unparser.Unparse(parseTree.Root.AstNode).WriteToStream(stream, unparser);

            ////string str = unparser.Unparse(parseTree.Root.AstNode).AsString(unparser);
            ////Console.WriteLine(str);
        }