Beispiel #1
0
        private EbnfTerm VisitTermNode(IInternalTreeNode node)
        {
            EbnfFactor factor = null;
            EbnfTerm   term   = null;

            for (int c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (child.NodeType)
                {
                case TreeNodeType.Internal:
                    var internalNode = child as IInternalTreeNode;
                    var symbolValue  = internalNode.Symbol.Value;
                    if (EbnfGrammar.Factor == symbolValue)
                    {
                        factor = VisitFactorNode(internalNode);
                    }
                    else if (EbnfGrammar.Term == symbolValue)
                    {
                        term = VisitTermNode(internalNode);
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            if (term == null)
            {
                return(new EbnfTerm(factor));
            }
            return(new EbnfTermConcatenation(factor, term));
        }
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
 public EbnfTermConcatenation(EbnfFactor factor, EbnfTerm term)
     : base(factor)
 {
     Term      = term;
     _hashCode = ComputeHashCode();
 }
Beispiel #4
0
 public EbnfTerm(EbnfFactor factor)
 {
     Factor    = factor;
     _hashCode = ComputeHashCode();
 }
        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 #6
0
 public EbnfTermConcatenation(EbnfFactor factor, EbnfTerm term)
     : base(factor)
 {
     Term = term;
     _hashCode = ComputeHashCode();
 }
Beispiel #7
0
 public EbnfTerm(EbnfFactor factor)
 {
     Factor = factor;
     _hashCode = ComputeHashCode();
 }