Exemple #1
0
 public EbnfDefinitionConcatenation(EbnfBlock block, EbnfDefinition definition)
     : base(block)
 {
     Assert.IsNotNull(definition, nameof(definition));
     Definition = definition;
     _hashCode = ComputeHashCode();
 }
        private void Definition(EbnfDefinition definition, GrammarModel grammarModel)
        {
            Block(definition.Block, grammarModel);

            if (definition.NodeType != EbnfNodeType.EbnfDefinitionConcatenation)
                return;

            var definitionConcatenation = definition as EbnfDefinitionConcatenation;
            Definition(definitionConcatenation.Definition, grammarModel);
        }
Exemple #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);
        }
Exemple #4
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);
 }
        public void EbnfGrammarGeneratorShouldCreateGrammarForGrouping()
        {
            // R = ( 'a' );
            var definition = new EbnfDefinition(
                new EbnfBlockRule(
                    new EbnfRule(
                        new EbnfQualifiedIdentifier("R"),
                        new EbnfExpression(
                            new EbnfTerm(
                                new EbnfFactorGrouping(
                                    new EbnfExpression(
                                        new EbnfTerm(
                                            new EbnfFactorLiteral("a")))))))));

            var grammar = GenerateGrammar(definition);
            Assert.IsNotNull(grammar);
            Assert.IsNotNull(grammar.Start);
            Assert.AreEqual(2, grammar.Productions.Count);
            Assert.AreEqual(1, grammar.Productions[0].RightHandSide.Count);
            Assert.AreEqual(1, grammar.Productions[1].RightHandSide.Count);
        }
        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);
        }
Exemple #7
0
 public override void Visit(IInternalTreeNode node)
 {
     if (EbnfGrammar.Definition == node.Symbol.Value)
         Definition = VisitDefinitionNode(node);
 }
 private static IGrammar GenerateGrammar(EbnfDefinition definition)
 {
     var generator = new EbnfGrammarGenerator();
     return generator.Generate(definition);
 }
 public void EbnfGrammarGeneratorShouldCreateGrammarForProductionAlteration()
 {
     // S = 'a' | 'b';
     var definition = new EbnfDefinition(
         new EbnfBlockRule(
             new EbnfRule(
                 new EbnfQualifiedIdentifier("S"),
                 new EbnfExpressionAlteration(
                     new EbnfTerm(
                         new EbnfFactorLiteral("a")),
                     new EbnfExpression(
                         new EbnfTerm(
                             new EbnfFactorLiteral("d")))))));
     var grammar = GenerateGrammar(definition);
     Assert.IsNotNull(grammar);
     Assert.IsNotNull(grammar.Start);
     Assert.AreEqual(2, grammar.Productions.Count);
 }
        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);
        }
 public IGrammar Generate(EbnfDefinition ebnf)
 {
     var grammarModel = new GrammarModel();
     Definition(ebnf, grammarModel);
     return grammarModel.ToGrammar();
 }
Exemple #12
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);
        }
Exemple #13
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);
        }
Exemple #14
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);
        }
Exemple #15
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);
        }