Пример #1
0
        public void EbnfParserShouldParseRegularExpression()
        {
            var expected = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                new EbnfFactorRegex(
                                    new Regex(
                                        false,
                                        new RegexExpressionTerm(
                                            new RegexTermFactor(
                                                new RegexFactorAtom(
                                                    new RegexAtomSet(
                                                        new RegexSet(new RegexCharacterClass(
                                                                         new RegexCharactersRange(
                                                                             new RegexCharacterClassCharacter('a'),
                                                                             new RegexCharacterClassCharacter('z'))),
                                                                     false))))),
                                        false)))))));

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

            Assert.AreEqual(expected, actual);
        }
Пример #2
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForOptional()
        {
            // R = ['a']
            var definition = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("R"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                new EbnfFactorOptional(
                                    new EbnfExpressionSimple(
                                        new EbnfTermSimple(
                                            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.Epsilon;

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

            Assert.AreEqual(expectedGrammar.Productions.Count, grammar.Productions.Count);
        }
Пример #3
0
        public void EbnfParserShouldParseCharacterProduction()
        {
            var expected = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                new EbnfFactorLiteral("a"))))));

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

            Assert.AreEqual(expected, actual);
        }
Пример #4
0
        public void EbnfParserShouldParseNamespace()
        {
            var expected = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("This", "Is", "A", "Qualifier", "Rule"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                new EbnfFactorLiteral("a"))))));

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

            Assert.AreEqual(expected, actual);
        }
Пример #5
0
        public void EbnfParserShouldParseSettings()
        {
            var actual = Parse(@"
                :ignore = whitespace; ");

            Assert.IsNotNull(actual);

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

            Assert.AreEqual(expected, actual);
        }
Пример #6
0
        public void EbnfParserShouldParseLexerRule()
        {
            var actual = Parse(@"
                b ~ 'b' ;");

            Assert.IsNotNull(actual);

            var expected = new EbnfDefinitionSimple(
                new EbnfBlockLexerRule(
                    new EbnfLexerRule(
                        new EbnfQualifiedIdentifier("b"),
                        new EbnfLexerRuleExpressionSimple(
                            new EbnfLexerRuleTermSimple(
                                new EbnfLexerRuleFactorLiteral("b"))))));

            Assert.AreEqual(expected, actual);
        }
Пример #7
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForSimpleRule()
        {
            // S = 'a';
            var definition = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("S"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                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].Count);
        }
Пример #8
0
        public void EbnfParserShouldParseAlterationAndConcatenation()
        {
            var expected = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("Rule"),
                        new EbnfExpressionAlteration(
                            new EbnfTermConcatenation(
                                new EbnfFactorLiteral("a"),
                                new EbnfTermSimple(
                                    new EbnfFactorLiteral("b"))),
                            new EbnfExpressionSimple(
                                new EbnfTermSimple(
                                    new EbnfFactorLiteral("c")))))));
            var actual = Parse(@"Rule = 'a' 'b' | 'c';");

            Assert.AreEqual(expected, actual);
        }
Пример #9
0
        public void EbnfGrammarGeneratorShouldCreateQualifiedName()
        {
            // X.Y.Z = 'a';
            var definition = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("X", "Y", "Z"),
                        new EbnfExpressionSimple(
                            new EbnfTermSimple(
                                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].Count);
            Assert.IsTrue(grammar.Productions[0].LeftHandSide.QualifiedName.Is("X.Y.Z"));
        }
Пример #10
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForMultipleOptionals()
        {
            // R = 'b' ['a'] 'c' ['d']
            var definition = new EbnfDefinitionSimple(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("R"),
                        new EbnfExpressionSimple(
                            new EbnfTermConcatenation(
                                new EbnfFactorLiteral("b"),
                                new EbnfTermConcatenation(
                                    new EbnfFactorOptional(
                                        new EbnfExpressionSimple(
                                            new EbnfTermSimple(
                                                new EbnfFactorLiteral("a")))),
                                    new EbnfTermConcatenation(
                                        new EbnfFactorLiteral("c"),
                                        new EbnfTermSimple(
                                            new EbnfFactorOptional(
                                                new EbnfExpressionSimple(
                                                    new EbnfTermSimple(
                                                        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.Epsilon;
            optD.Rule = 'd' | Expr.Epsilon;

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

            Assert.AreEqual(expectedGrammar.Productions.Count, grammar.Productions.Count);
        }