示例#1
0
 public void GrammarModelShouldAddProductionModel()
 {
     var grammar = new GrammarModel();
     grammar.Productions.Add(
         new ProductionModel(""));
     Assert.AreEqual(1, grammar.Productions.Count);
 }
示例#2
0
        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);
        }
示例#3
0
        public void GrammarModelConstructorGivenOnlyStartProductionShouldTraverseRecursiveStructureOnlyOnce()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            S.AddWithAnd(S);
            S.AddWithOr(A);
            A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a")));

            var grammarModel = new GrammarModel(S);
            var grammar = grammarModel.ToGrammar();

            Assert.AreEqual(3, grammar.Productions.Count);
        }
示例#4
0
        void Block(EbnfBlock block, GrammarModel grammarModel)
        {
            switch (block.NodeType)
            {
                case EbnfNodeType.EbnfBlockLexerRule:
                    break;

                case EbnfNodeType.EbnfBlockRule:
                    var blockRule = block as EbnfBlockRule;
                    foreach (var production in Rule(blockRule.Rule))
                        grammarModel.Productions.Add(production);
                    break;

                case EbnfNodeType.EbnfBlockSetting:
                    break;
            }
        }
示例#5
0
        public void GrammarModelConstructorGivenOnlyStartProductionShouldDiscoverLinkedProductions()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");
            var C = new ProductionModel("C");
            S.AddWithAnd(A);
            A.AddWithAnd(B);
            A.AddWithOr(C);
            B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b")));
            C.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("c")));

            var grammarModel = new GrammarModel(S);
            var grammar = grammarModel.ToGrammar();

            Assert.AreEqual(5, grammar.Productions.Count);
        }
示例#6
0
        public GrammarExpression(
            ProductionExpression start,
            IEnumerable<ProductionExpression> productions,
            IEnumerable<ILexerRule> ignore)
        {
            GrammarModel = new GrammarModel
            {
                Start = start.ProductionModel
            };

            if (productions != null)
                foreach (var production in productions)
                    GrammarModel.Productions.Add(production.ProductionModel);
            if (ignore != null)
                foreach (var ignoreRule in ignore)
                    GrammarModel.IgnoreRules.Add(new LexerRuleModel(ignoreRule));
        }
示例#7
0
        public void GrammarModelGivenNullStartShouldResolveStartFromProductions()
        {
            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");

            S.AddWithAnd(A);
            S.AddWithAnd(B);
            A.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("a")));
            A.AddWithAnd(B);
            B.AddWithAnd(new LexerRuleModel(new StringLiteralLexerRule("b")));

            var grammarModel = new GrammarModel();
            grammarModel.Productions.Add(S);
            grammarModel.Productions.Add(A);
            grammarModel.Productions.Add(B);

            var grammar = grammarModel.ToGrammar();
            Assert.AreEqual(3, grammar.Productions.Count);
            Assert.IsNotNull(grammar.Start);
        }
示例#8
0
 public IGrammar Generate(EbnfDefinition ebnf)
 {
     var grammarModel = new GrammarModel();
     Definition(ebnf, grammarModel);
     return grammarModel.ToGrammar();
 }
示例#9
0
        public void GrammarModelToGrammarShouldCreateGrammar()
        {
            var grammarModel = new GrammarModel();

            var S = new ProductionModel("S");
            var A = new ProductionModel("A");
            var B = new ProductionModel("B");

            var a = new StringLiteralLexerRule("a");
            var b = new StringLiteralLexerRule("b");
            var space = new StringLiteralLexerRule(" ");

            S.AddWithAnd(A.LeftHandSide);
            S.AddWithAnd(B.LeftHandSide);
            S.AddWithOr(B.LeftHandSide);
            A.AddWithAnd(new LexerRuleModel(a));
            B.AddWithAnd(new LexerRuleModel(b));

            grammarModel.Productions.Add(S);
            grammarModel.Productions.Add(A);
            grammarModel.Productions.Add(B);

            grammarModel.IgnoreRules.Add(new LexerRuleModel(space));

            grammarModel.Start = S;

            var grammar = grammarModel.ToGrammar();
            Assert.AreEqual(4, grammar.Productions.Count);
            Assert.AreEqual(1, grammar.Ignores.Count);
        }
示例#10
0
 public void GrammarModelToGrammarShouldAddProductionWhenEmptyDefinition()
 {
     var S = new ProductionModel("S");
     var grammarModel = new GrammarModel(S);
     var grammar = grammarModel.ToGrammar();
     Assert.AreEqual(1, grammar.Productions.Count);
 }
示例#11
0
 public void GrammarModelShouldAddIgnoreLexerRuleModel()
 {
     var grammar = new GrammarModel();
     grammar.IgnoreRules.Add(new LexerRuleModel {  Value = new StringLiteralLexerRule("this is a literal")});
     Assert.AreEqual(1, grammar.IgnoreRules.Count);
 }
示例#12
0
        public void GrammarModelToGrammarShouldResolverProductionReferencesFromOtherGrammars()
        {
            var S = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "S")) };
            var A = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns1", "A")) };
            S.Alterations.Add(
                new AlterationModel(
                    new[] { A }));
            A.Alterations.Add(
                new AlterationModel(
                    new[] { new LexerRuleModel(
                        new StringLiteralLexerRule("a"))})
            );
            var ns1GrammarModel = new GrammarModel
            {
                Start = S
            };
            ns1GrammarModel.Productions.Add(S);
            ns1GrammarModel.Productions.Add(A);

            var ns1ProductionReferece = new ProductionReferenceModel(ns1GrammarModel.ToGrammar());

            var Z = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "Z")) };
            var X = new ProductionModel { LeftHandSide = new NonTerminalModel(new FullyQualifiedName("ns2", "X")) };
            X.Alterations.Add(
                new AlterationModel(
                    new SymbolModel[]
                    {
                        Z, ns1ProductionReferece
                    }));

            var ns2GrammarModel = new GrammarModel
            {
                Start = Z
            };
            ns2GrammarModel.Productions.Add(Z);
            ns2GrammarModel.Productions.Add(X);

            var ns2Grammar = ns2GrammarModel.ToGrammar();

            Assert.AreEqual(4, ns2Grammar.Productions.Count);
        }