예제 #1
0
        private PdlTerm VisitTermNode(IInternalTreeNode node)
        {
            PdlFactor factor = null;
            PdlTerm   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 (PdlGrammar.Factor == symbolValue)
                    {
                        factor = VisitFactorNode(internalNode);
                    }
                    else if (PdlGrammar.Term == symbolValue)
                    {
                        term = VisitTermNode(internalNode);
                    }
                    break;

                case TreeNodeType.Token:
                    break;
                }
            }
            if (term is null)
            {
                return(new PdlTerm(factor));
            }
            return(new PdlTermConcatenation(factor, term));
        }
예제 #2
0
        IEnumerable <ProductionModel> Factor(PdlFactor factor, ProductionModel currentProduction)
        {
            switch (factor.NodeType)
            {
            case PdlNodeType.PdlFactorGrouping:
                var grouping = factor as PdlFactorGrouping;
                foreach (var production in Grouping(grouping, currentProduction))
                {
                    yield return(production);
                }
                break;

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

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

            case PdlNodeType.PdlFactorIdentifier:
                var identifier  = factor as PdlFactorIdentifier;
                var nonTerminal = GetFullyQualifiedNameFromQualifiedIdentifier(identifier.QualifiedIdentifier);
                currentProduction.AddWithAnd(new NonTerminalModel(nonTerminal));
                break;

            case PdlNodeType.PdlFactorLiteral:
                var literal           = factor as PdlFactorLiteral;
                var stringLiteralRule = new StringLiteralLexerRule(literal.Value.ToString());
                currentProduction.AddWithAnd(new LexerRuleModel(stringLiteralRule));
                break;

            case PdlNodeType.PdlFactorRegex:
                var regex        = factor as PdlFactorRegex;
                var nfa          = _regexToNfaAlgorithm.Transform(regex.Regex);
                var dfa          = _nfaToDfaAlgorithm.Transform(nfa);
                var dfaLexerRule = new DfaLexerRule(dfa, regex.Regex.ToString());
                currentProduction.AddWithAnd(new LexerRuleModel(dfaLexerRule));
                break;
            }
        }
예제 #3
0
 public PdlTermConcatenation(PdlFactor factor, PdlTerm term)
     : base(factor)
 {
     Term      = term;
     _hashCode = ComputeHashCode();
 }
예제 #4
0
 public PdlTerm(PdlFactor factor)
 {
     Factor    = factor;
     _hashCode = ComputeHashCode();
 }