Ejemplo n.º 1
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForOptional()
        {
            // R = ['a']
            var definition = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("R"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorOptional(
                                    new EbnfExpression(
                                        new EbnfTerm(
                                            new EbnfFactorLiteral("a")))))))));

            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);

            ProductionExpression
                R    = "R",
                optA = "[a]";

            R.Rule    = optA;
            optA.Rule = 'a'
                        | (Expr)null;

            var expectedGrammar = new GrammarExpression(R, new[] { R, optA }).ToGrammar();

            Assert.AreEqual(expectedGrammar.Productions.Count, grammar.Productions.Count);
        }
Ejemplo n.º 2
0
        public void EbnfParserShouldParseRegularExpression()
        {
            var expected = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorRegex(
                                    new Regex(
                                        startsWith: false,
                                        expression: new RegexExpressionTerm(
                                            new RegexTerm(
                                                new RegexFactor(
                                                    new RegexAtomSet(
                                                        new RegexSet(false,
                                                                     new RegexCharacterClass(
                                                                         new RegexCharacterRange(
                                                                             new RegexCharacterClassCharacter('a'),
                                                                             new RegexCharacterClassCharacter('z')))))))),
                                        endsWith: false)))))));

            var actual = Parse(@"Rule = /[a-z]/;");

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void EbnfParserShouldParseCharacterProduction()
        {
            var expected = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorLiteral("a"))))));

            var actual = Parse(@"Rule = 'a';");

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void EbnfParserShouldParseSettings()
        {
            var actual = Parse(@"
                :ignore = whitespace; ");

            Assert.IsNotNull(actual);

            var expected = new EbnfDefinition(
                new EbnfBlockSetting(
                    new EbnfSetting(
                        new EbnfSettingIdentifier(":ignore"),
                        new EbnfQualifiedIdentifier("whitespace"))));

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForSimpleRule()
        {
            // S = 'a';
            var definition = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("S"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorLiteral("a"))))));
            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(1, grammar.Productions.Count);
            Assert.AreEqual(1, grammar.Productions[0].RightHandSide.Count);
        }
Ejemplo n.º 6
0
        public void EbnfParserShouldParseLexerRule()
        {
            var actual = Parse(@"
                b ~ 'b' ;");

            Assert.IsNotNull(actual);

            var expected = new EbnfDefinition(
                block: new EbnfBlockLexerRule(
                    lexerRule: new EbnfLexerRule(
                        qualifiedIdentifier: new EbnfQualifiedIdentifier("b"),
                        expression:  new EbnfLexerRuleExpression(
                            term: new EbnfLexerRuleTerm(
                                factor: new EbnfLexerRuleFactorLiteral("b"))))));

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void EbnfParserShouldParseAlterationAndConcatenation()
        {
            var expected = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpressionAlteration(
                            new EbnfTermConcatenation(
                                new EbnfFactorLiteral("a"),
                                new EbnfTerm(
                                    new EbnfFactorLiteral("b"))),
                            new EbnfExpression(
                                new EbnfTerm(
                                    new EbnfFactorLiteral("c")))))));
            var actual = Parse(@"Rule = 'a' 'b' | 'c';");

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void EbnfParserShouldParseNamespace()
        {
            var expected = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifierConcatenation("This",
                                                                 new EbnfQualifiedIdentifierConcatenation("Is",
                                                                                                          new EbnfQualifiedIdentifierConcatenation("A",
                                                                                                                                                   new EbnfQualifiedIdentifierConcatenation("Namespace",
                                                                                                                                                                                            new EbnfQualifiedIdentifier("Rule"))))),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorLiteral("a"))))));

            var actual = Parse(@"This.Is.A.Namespace.Rule = 'a'; ");

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 9
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForMultipleOptionals()
        {
            // R = 'b' ['a'] 'c' ['d']
            var definition = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("R"),
                        new EbnfExpression(
                            new EbnfTermConcatenation(
                                new EbnfFactorLiteral("b"),
                                new EbnfTermConcatenation(
                                    new EbnfFactorOptional(
                                        new EbnfExpression(
                                            new EbnfTerm(
                                                new EbnfFactorLiteral("a")))),
                                    new EbnfTermConcatenation(
                                        new EbnfFactorLiteral("c"),
                                        new EbnfTerm(
                                            new EbnfFactorOptional(
                                                new EbnfExpression(
                                                    new EbnfTerm(
                                                        new EbnfFactorLiteral("d"))))))))))));
            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);

            ProductionExpression
                R    = "R",
                optA = "[a]",
                optD = "[d]";

            R.Rule =
                (Expr)'b' + optA + 'c' + optD;
            optA.Rule = 'a' | (Expr)null;
            optD.Rule = 'd' | (Expr)null;

            var expectedGrammar = new GrammarExpression(R, new[] { R, optA, optD }).ToGrammar();

            Assert.AreEqual(expectedGrammar.Productions.Count, grammar.Productions.Count);
        }
Ejemplo n.º 10
0
        private static IGrammar GenerateGrammar(EbnfDefinition definition)
        {
            var generator = new EbnfGrammarGenerator();

            return(generator.Generate(definition));
        }