public void PdlGrammarGeneratorShouldCreateGrammarForMultipleProductionsWithAlterations()
        {
            // S = 'a' | 'd';
            // S = 'b' | 'c';
            var definition = new PdlDefinitionConcatenation(
                new PdlBlockRule(
                    new PdlRule(
                        new PdlQualifiedIdentifier("S"),
                        new PdlExpressionAlteration(
                            new PdlTerm(
                                new PdlFactorLiteral("a")),
                            new PdlExpression(
                                new PdlTerm(
                                    new PdlFactorLiteral("d")))))),
                new PdlDefinition(
                    new PdlBlockRule(
                        new PdlRule(
                            new PdlQualifiedIdentifier("S"),
                            new PdlExpressionAlteration(
                                new PdlTerm(
                                    new PdlFactorLiteral("b")),
                                new PdlExpression(
                                    new PdlTerm(
                                        new PdlFactorLiteral("c"))))))));
            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(4, grammar.Productions.Count);
        }
        public void PdlGeneratorShouldGenerateTrivia()
        {
            var whiteSpaceRegex = new RegexDefinition(
                false,
                new RegexExpressionTerm(
                    new RegexTerm(
                        new RegexFactorIterator(
                            new RegexAtomSet(
                                new RegexSet(
                                    false,
                                    new RegexCharacterClass(
                                        new RegexCharacterUnitRange(
                                            new RegexCharacterClassCharacter(' '))))),
                            RegexIterator.OneOrMany))),
                false);
            var definition = new PdlDefinitionConcatenation(
                new PdlBlockRule(
                    new PdlRule(
                        new PdlQualifiedIdentifier("S"),
                        new PdlExpression(
                            new PdlTerm(
                                new PdlFactorLiteral("a"))))),
                new PdlDefinitionConcatenation(
                    new PdlBlockLexerRule(
                        new PdlLexerRule(
                            new PdlQualifiedIdentifier("whitespace"),
                            new PdlLexerRuleExpression(
                                new PdlLexerRuleTerm(
                                    new PdlLexerRuleFactorRegex(
                                        whiteSpaceRegex))))),
                    new PdlDefinition(
                        new PdlBlockSetting(
                            new PdlSetting(
                                new PdlSettingIdentifier("trivia"),
                                new PdlQualifiedIdentifier("whitespace"))))));

            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar.Trivia);
            Assert.AreEqual(1, grammar.Trivia.Count);
        }
        public void PdlGrammarGeneratorStartSettingShouldSetStartProduction()
        {
            // :start S;
            // S = 'a';
            var definition = new PdlDefinitionConcatenation(
                new PdlBlockSetting(
                    new PdlSetting(
                        new PdlSettingIdentifier("start"),
                        new PdlQualifiedIdentifier("S"))),
                new PdlDefinition(
                    new PdlBlockRule(
                        new PdlRule(
                            new PdlQualifiedIdentifier("S"),
                            new PdlExpression(
                                new PdlTerm(
                                    new PdlFactorLiteral("a")))))));


            var grammar = GenerateGrammar(definition);

            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(grammar.Start.FullyQualifiedName.Name, "S");
        }
Beispiel #4
0
        public void PdlParserShouldParseMultipleRules()
        {
            var expected = new PdlDefinitionConcatenation(
                new PdlBlockRule(
                    new PdlRule(
                        new PdlQualifiedIdentifier("S"),
                        new PdlExpression(
                            new PdlTermConcatenation(
                                new PdlFactorIdentifier(
                                    new PdlQualifiedIdentifier("A")),
                                new PdlTerm(
                                    new PdlFactorIdentifier(
                                        new PdlQualifiedIdentifier("B"))))))),
                new PdlDefinitionConcatenation(
                    new PdlBlockRule(
                        new PdlRule(
                            new PdlQualifiedIdentifier("A"),
                            new PdlExpression(
                                new PdlTerm(
                                    new PdlFactorLiteral("a"))))),
                    new PdlDefinition(
                        new PdlBlockRule(
                            new PdlRule(
                                new PdlQualifiedIdentifier(
                                    "B"),
                                new PdlExpression(
                                    new PdlTerm(
                                        new PdlFactorLiteral("b"))))))));
            var actual = Parse(@"
                S = A B;
                A = 'a';
                B = 'b';
            ");

            Assert.AreEqual(expected, actual);
        }