Exemplo n.º 1
0
        public void EbnfGrammarGeneratorShouldCreateGrammarForMultipleProductionsWithAlterations()
        {
            // S = 'a' | 'd';
            // S = 'b' | 'c';
            var definition = new EbnfDefinitionConcatenation(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("S"),
                        new EbnfExpressionAlteration(
                            new EbnfTerm(
                                new EbnfFactorLiteral("a")),
                            new EbnfExpression(
                                new EbnfTerm(
                                    new EbnfFactorLiteral("d")))))),
                new EbnfDefinition(
                    new EbnfBlockRule(
                        new EbnfRule(
                            new EbnfQualifiedIdentifier("S"),
                            new EbnfExpressionAlteration(
                                new EbnfTerm(
                                    new EbnfFactorLiteral("b")),
                                new EbnfExpression(
                                    new EbnfTerm(
                                        new EbnfFactorLiteral("c"))))))));
            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(4, grammar.Productions.Count);
        }
Exemplo n.º 2
0
        public void EbnfGeneratorShouldGenerateTrivia()
        {
            var whiteSpaceRegex = new Regex(
                false,
                new RegexExpressionTerm(
                    new RegexTerm(
                        new RegexFactorIterator(
                            new RegexAtomSet(
                                new RegexSet(
                                    false,
                                    new RegexCharacterClass(
                                        new RegexCharacterUnitRange(
                                            new RegexCharacterClassCharacter(' '))))),
                            RegexIterator.OneOrMany))),
                false);
            var definition = new EbnfDefinitionConcatenation(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("S"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorLiteral("a"))))),
                new EbnfDefinitionConcatenation(
                    new EbnfBlockLexerRule(
                        new EbnfLexerRule(
                            new EbnfQualifiedIdentifier("whitespace"),
                            new EbnfLexerRuleExpression(
                                new EbnfLexerRuleTerm(
                                    new EbnfLexerRuleFactorRegex(
                                        whiteSpaceRegex))))),
                    new EbnfDefinition(
                        new EbnfBlockSetting(
                            new EbnfSetting(
                                new EbnfSettingIdentifier("trivia"),
                                new EbnfQualifiedIdentifier("whitespace"))))));

            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar.Trivia);
            Assert.AreEqual(1, grammar.Trivia.Count);
        }
Exemplo n.º 3
0
        public void EbnfGrammarGeneratorStartSettingShouldSetStartProduction()
        {
            // :start S;
            // S = 'a';
            var definition = new EbnfDefinitionConcatenation(
                new EbnfBlockSetting(
                    new EbnfSetting(
                        new EbnfSettingIdentifier("start"),
                        new EbnfQualifiedIdentifier("S"))),
                new EbnfDefinition(
                    new EbnfBlockRule(
                        new EbnfRule(
                            new EbnfQualifiedIdentifier("S"),
                            new EbnfExpression(
                                new EbnfTerm(
                                    new EbnfFactorLiteral("a")))))));


            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(grammar.Start.FullyQualifiedName.Name, "S");
        }
Exemplo n.º 4
0
        public void EbnfParserShouldParseMultipleRules()
        {
            var expected = new EbnfDefinitionConcatenation(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("S"),
                        new EbnfExpressionSimple(
                            new EbnfTermConcatenation(
                                new EbnfFactorIdentifier(
                                    new EbnfQualifiedIdentifier("A")),
                                new EbnfTermSimple(
                                    new EbnfFactorIdentifier(
                                        new EbnfQualifiedIdentifier("B"))))))),
                new EbnfDefinitionConcatenation(
                    new EbnfBlockRule(
                        new EbnfRule(
                            new EbnfQualifiedIdentifier("A"),
                            new EbnfExpressionSimple(
                                new EbnfTermSimple(
                                    new EbnfFactorLiteral("a"))))),
                    new EbnfDefinitionSimple(
                        new EbnfBlockRule(
                            new EbnfRule(
                                new EbnfQualifiedIdentifier(
                                    "B"),
                                new EbnfExpressionSimple(
                                    new EbnfTermSimple(
                                        new EbnfFactorLiteral("b"))))))));
            var actual = Parse(@"
                S = A B;
                A = 'a';
                B = 'b';
            ");

            Assert.AreEqual(expected, actual);
        }