public void TestCanParseLargeJsonFileWithCompression()
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "10000.json");

            using var stream = File.OpenRead(path);
            using var reader = new StreamReader(stream);
            _compressedParseTester.RunParse(reader);
        }
Beispiel #2
0
        public void TestCanParseLargeJsonFileWithCompression()
        {
            var path = Path.Combine(TestContext.TestDeploymentDir, "Runtime", "10000.json");

            using (var stream = File.OpenRead(path))
                using (var reader = new StreamReader(stream))
                {
                    _compressedParseTester.RunParse(reader);
                }
        }
Beispiel #3
0
        public void TestCanParseLargeJsonFileWithMarpa()
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "10000.json");

            using (var stream = File.OpenRead(path))
                using (var reader = new StreamReader(stream))
                {
                    _marpaParseTester.RunParse(reader);
                }
        }
        public void ParseTreeEnumeratorShouldEnumerateMultipleTrees()
        {
            var parseTester = new ParseTester(new SimpleExpressionGrammar());

            parseTester.RunParse("3+2*1+1");
            var internalForestNode  = parseTester.ParseEngine.GetParseForestRootNode();
            var parseTreeEnumerable = new ParseTreeEnumerable(internalForestNode);

            var count = 0;

            foreach (var parseTree in parseTreeEnumerable)
            {
                count++;
            }
            Assert.AreEqual(4, count);
        }
        public void EbnfGrammarGeneratorShouldCreateGrammarForComplexDefinition()
        {
            var ebnf =
                @"file = ws directives ws ;
                ws = [ ows ] ; /* white space */
                ows = ""_""; /* obligatory white space */
                directives = directive { ows directive };
                directive = ""0"" | ""1""; ";

            var parser           = new EbnfParser();
            var ebnfDefinition   = parser.Parse(ebnf);
            var generatedGrammar = GenerateGrammar(ebnfDefinition);

            Assert.IsNotNull(generatedGrammar);
            var parseEngine = new ParseEngine(generatedGrammar, new ParseEngineOptions(optimizeRightRecursion: true));
            var parseTester = new ParseTester(parseEngine);

            parseTester.RunParse("_0_1_0_0_1_1_");
        }
Beispiel #6
0
        public void ParseEngineShouldDisambiguateFollowingOperatorPresidence()
        {
            var input       = "2*3+5*7";
            var parseTester = new ParseTester(new ExpressionGrammar());

            parseTester.RunParse(input);
            var forest = parseTester.ParseEngine.GetParseForestRootNode();
            var tree   = new InternalTreeNode(forest);

            // ((2*3)+(5*7))
            // (E, 0, 7) = (E, 0, 5) ('*', 5, 6) (E, 6, 7)
            // (E, 0, 5) = (E, 0, 3) ('+', 3, 4) (E, 4, 5)
            // (E, 0, 3) = (E, 0, 1) ('*', 1, 2) (E, 2, 3)
            // (E, 0, 1) = ('2', 0, 1)
            // (E, 2, 3) = ('3', 2, 3)
            // (E, 4, 5) = ('5', 4, 5)
            // (E, 6, 7) = ('7', 6, 7)
            Assert.Inconclusive();
        }
Beispiel #7
0
        private static void AssertLeoAndClassicParseAlgorithmsCreateSameForest(string input, IGrammar grammar)
        {
            var leoEngine     = new ParseEngine(grammar);
            var classicEngine = new ParseEngine(grammar, new ParseEngineOptions(optimizeRightRecursion: false));

            var leoTester     = new ParseTester(leoEngine);
            var classicTester = new ParseTester(classicEngine);

            leoTester.RunParse(input);
            classicTester.RunParse(input);

            var nodeComparer           = new StatefulForestNodeComparer();
            var leoParseForestRoot     = leoEngine.GetParseForestRootNode();
            var classicParseForestRoot = classicEngine.GetParseForestRootNode();

            Assert.IsTrue(nodeComparer.Equals(
                              classicParseForestRoot,
                              leoParseForestRoot),
                          "Leo and Classic Parse Forest mismatch");
        }
        public void DeterministicParseEngineShouldParseInSubCubicTimeGivenRightRecursiveGrammar()
        {
            var a = new TerminalLexerRule(
                new CharacterTerminal('a'),
                new TokenType("a"));
            ProductionExpression A = "A";

            A.Rule =
                'a' + A
                | (Expr)null;

            var grammarExpression = new GrammarExpression(A, new[] { A });

            var parseTester = new ParseTester(
                new DeterministicParseEngine(
                    new PreComputedGrammar(grammarExpression.ToGrammar())));

            const string input = "aaaaa";

            parseTester.RunParse(input);
        }
        public void DeterministicParseEngineShouldParseGrammarWithCycles()
        {
            ProductionExpression
                A = nameof(A),
                B = nameof(B),
                C = nameof(C);

            A.Rule = B | 'a';
            B.Rule = C | 'b';
            C.Rule = A | 'c';

            var grammar = new GrammarExpression(A, new[] { A, B, C })
                          .ToGrammar();

            var parseTester = new ParseTester(
                new DeterministicParseEngine(
                    new PreComputedGrammar(grammar)));

            const string input = "a";

            parseTester.RunParse(input);
        }
Beispiel #10
0
        private static Tuple <IToken, IToken> RunTriviaTestRepeatingWordGrammarParse(string input)
        {
            var parseTester = new ParseTester(_repeatingWordGrammar);

            parseTester.RunParse(input);
            var parseForestRoot = parseTester.ParseEngine.GetParseForestRootNode();

            var firstTokenForestNode = parseForestRoot
                                       .AssertInBoundsAndNavigate(andNode => andNode.Children, 0)
                                       .AssertInBoundsAndNavigate(forestNode => forestNode.Children, 0)
                                       .AssertIsInstanceOfTypeAndCast <ITokenForestNode>();

            var secondTokenForestNode = parseForestRoot
                                        .AssertInBoundsAndNavigate(andNode => andNode.Children, 0)
                                        .AssertInBoundsAndNavigate(forestNode => forestNode.Children, 1)
                                        .AssertIsInstanceOfTypeAndCast <IInternalForestNode>()
                                        .AssertInBoundsAndNavigate(internalForestNode => internalForestNode.Children, 0)
                                        .AssertInBoundsAndNavigate(andNode => andNode.Children, 0)
                                        .AssertIsInstanceOfTypeAndCast <ITokenForestNode>();

            return(new Tuple <IToken, IToken>(firstTokenForestNode.Token, secondTokenForestNode.Token));
        }
Beispiel #11
0
        public void TestCanParseJsonArray()
        {
            var json = @"[""one"", ""two""]";

            _parseTester.RunParse(json);
        }
Beispiel #12
0
        private static void ParseInput(IParseEngine parseEngine, IReadOnlyList <IToken> tokens)
        {
            var parseTester = new ParseTester(parseEngine);

            parseTester.RunParse(tokens);
        }