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);
        }
Beispiel #2
0
        IEnumerable <ProductionModel> Factor(EbnfFactor factor, ProductionModel currentProduction)
        {
            switch (factor.NodeType)
            {
            case EbnfNodeType.EbnfFactorGrouping:
                var grouping = factor as EbnfFactorGrouping;
                foreach (var production in Grouping(grouping, currentProduction))
                {
                    yield return(production);
                }
                break;

            case EbnfNodeType.EbnfFactorOptional:
                var optional = factor as EbnfFactorOptional;
                foreach (var production in Optional(optional, currentProduction))
                {
                    yield return(production);
                }
                break;

            case EbnfNodeType.EbnfFactorRepetition:
                var repetition = factor as EbnfFactorRepetition;
                foreach (var production in Repetition(repetition, currentProduction))
                {
                    yield return(production);
                }
                break;

            case EbnfNodeType.EbnfFactorIdentifier:
                var identifier  = factor as EbnfFactorIdentifier;
                var nonTerminal = GetNonTerminalFromQualifiedIdentifier(identifier.QualifiedIdentifier);
                currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
                break;

            case EbnfNodeType.EbnfFactorLiteral:
                var literal           = factor as EbnfFactorLiteral;
                var stringLiteralRule = new StringLiteralLexerRule(literal.Value);
                currentProduction.AddWithAnd(new LexerRuleModel(stringLiteralRule));
                break;

            case EbnfNodeType.EbnfFactorRegex:
                var regex        = factor as EbnfFactorRegex;
                var nfa          = _thompsonConstructionAlgorithm.Transform(regex.Regex);
                var dfa          = _subsetConstructionAlgorithm.Transform(nfa);
                var dfaLexerRule = new DfaLexerRule(dfa, regex.Regex.ToString());
                currentProduction.AddWithAnd(new LexerRuleModel(dfaLexerRule));
                break;
            }
        }
Beispiel #3
0
        private IEnumerable <ProductionModel> Factor(IEbnfFactor factor, ProductionModel currentProduction)
        {
            switch (factor)
            {
            case EbnfFactorGrouping grouping:
                foreach (var production in Grouping(grouping, currentProduction))
                {
                    yield return(production);
                }

                break;

            case EbnfFactorOptional optional:
                foreach (var production in Optional(optional, currentProduction))
                {
                    yield return(production);
                }

                break;

            case EbnfFactorRepetition repetition:
                foreach (var production in Repetition(repetition, currentProduction))
                {
                    yield return(production);
                }

                break;

            case EbnfFactorIdentifier identifier:
                var nonTerminal = NonTerminal.From(GetQualifiedName(identifier.QualifiedIdentifier));
                currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
                break;

            case EbnfFactorLiteral literal:
                var stringLiteralRule = new StringLiteralLexerRule(literal.Value);
                currentProduction.AddWithAnd(new LexerRuleModel(stringLiteralRule));
                break;

            case EbnfFactorRegex regex:
                var nfa          = this.regexToNfa.Transform(regex.Regex);
                var dfa          = this.nfaToDfa.Transform(nfa);
                var dfaLexerRule = new DfaLexerRule(dfa, regex.Regex.ToString());
                currentProduction.AddWithAnd(new LexerRuleModel(dfaLexerRule));
                break;
            }
        }
        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);
        }
        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);
        }
Beispiel #6
0
        IEnumerable <ProductionModel> Grouping(EbnfFactorGrouping grouping, ProductionModel currentProduction)
        {
            var name               = grouping.ToString();
            var nonTerminal        = new NonTerminal(name);
            var groupingProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = grouping.Expression;

            foreach (var production in Expression(expression, groupingProduction))
            {
                yield return(production);
            }

            yield return(groupingProduction);
        }
        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);
        }
Beispiel #8
0
        IEnumerable <ProductionModel> Optional(EbnfFactorOptional optional, ProductionModel currentProduction)
        {
            var name               = optional.ToString();
            var nonTerminal        = new NonTerminal(name);
            var optionalProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = optional.Expression;

            foreach (var production in Expression(expression, optionalProduction))
            {
                yield return(production);
            }

            optionalProduction.Lambda();
            yield return(optionalProduction);
        }
Beispiel #9
0
        IEnumerable <ProductionModel> Repetition(EbnfFactorRepetition repetition, ProductionModel currentProduction)
        {
            var name                 = repetition.ToString();
            var nonTerminal          = new NonTerminal(name);
            var repetitionProduction = new ProductionModel(nonTerminal);

            currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));

            var expression = repetition.Expression;

            foreach (var production in Expression(expression, repetitionProduction))
            {
                yield return(production);
            }

            repetitionProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
            repetitionProduction.Lambda();

            yield return(repetitionProduction);
        }