Ejemplo n.º 1
0
        private RegexExpression VisitRegexExpressionNode(IInternalTreeNode node)
        {
            RegexExpression expression = null;
            IRegexTerm      term       = null;

            foreach (var internalNode in node.Children.OfType <IInternalTreeNode>())
            {
                if (internalNode.Is(RegexGrammar.Expression))
                {
                    expression = VisitRegexExpressionNode(internalNode);
                }
                else if (internalNode.Is(RegexGrammar.Term))
                {
                    term = VisitRegexTermNode(internalNode);
                }
            }

            if (expression != null && term != null)
            {
                return(new RegexExpressionAlteration(term, expression));
            }

            if (term != null)
            {
                return(new RegexExpressionTerm(term));
            }

            throw new InvalidOperationException("Unable to create null expression.");
        }
Ejemplo n.º 2
0
 public Regex(
     bool startsWith,
     RegexExpression expression,
     bool endsWith)
 {
     StartsWith = startsWith;
     EndsWith   = endsWith;
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }
Ejemplo n.º 3
0
        private static Nfa Expression(RegexExpression expression)
        {
            switch (expression)
            {
            case RegexExpressionAlteration alteration:
                var termNfa       = Term(alteration.Term);
                var expressionNfa = Expression(alteration.Expression);
                return(Union(termNfa, expressionNfa));

            case RegexExpressionTerm term:
                return(Term(term.Term));

            case RegexExpression _:
                return(Empty());
            }

            throw new InvalidOperationException("Unrecognized Regex Expression");
        }
        private static INfa Expression(RegexExpression expression)
        {
            switch (expression.NodeType)
            {
            case RegexNodeType.RegexExpression:
                return(Empty());

            case RegexNodeType.RegexExpressionAlteration:
                var regexExpressionAlteration = expression as RegexExpressionAlteration;

                var termNfa       = Term(regexExpressionAlteration.Term);
                var expressionNfa = Expression(regexExpressionAlteration.Expression);

                return(Union(termNfa, expressionNfa));

            case RegexNodeType.RegexExpressionTerm:
                var regexExpressionTerm = expression as RegexExpressionTerm;
                return(Term(regexExpressionTerm.Term));
            }
            throw new InvalidOperationException("Unrecognized Regex Expression");
        }
Ejemplo n.º 5
0
        private Regex VisitRegexNode(IInternalTreeNode node)
        {
            RegexExpression expression = null;
            var             startsWith = false;
            var             endsWith   = false;

            for (var c = 0; c < node.Children.Count; c++)
            {
                var child = node.Children[c];
                switch (node.NodeType)
                {
                case TreeNodeType.Internal:

                    var internalNode            = child as IInternalTreeNode;
                    var internalNodeSymbolValue = internalNode.Symbol.Value;
                    if (RegexGrammar.Expression == internalNodeSymbolValue)
                    {
                        expression = VisitRegexExpressionNode(internalNode);
                    }

                    break;

                case TreeNodeType.Token:
                    var tokenNode = child as ITokenTreeNode;
                    switch (tokenNode.Token.Value)
                    {
                    case "$":
                        endsWith = true;
                        break;

                    case "^":
                        startsWith = true;
                        break;
                    }
                    break;
                }
            }
            return(new Regex(startsWith, expression, endsWith));
        }
Ejemplo n.º 6
0
        private RegexExpression VisitRegexExpressionNode(IInternalTreeNode internalNode)
        {
            RegexExpression expression = null;
            RegexTerm       term       = null;

            for (var c = 0; c < internalNode.Children.Count; c++)
            {
                var child = internalNode.Children[c];
                if (child.NodeType != TreeNodeType.Internal)
                {
                    continue;
                }
                var childInternalNode            = child as IInternalTreeNode;
                var childInternalNodeSymbolValue = childInternalNode.Symbol.Value;

                if (RegexGrammar.Expression == childInternalNodeSymbolValue)
                {
                    expression = VisitRegexExpressionNode(childInternalNode);
                }

                else if (RegexGrammar.Term == childInternalNodeSymbolValue)
                {
                    term = VisitRegexTermNode(childInternalNode);
                }
            }

            if (expression != null && term != null)
            {
                return(new RegexExpressionAlteration(term, expression));
            }

            if (term != null)
            {
                return(new RegexExpressionTerm(term));
            }

            throw new InvalidOperationException("Unable to create null expression.");
        }
Ejemplo n.º 7
0
        private Regex VisitRegexNode(IInternalTreeNode node)
        {
            RegexExpression expression = null;
            var             startsWith = false;
            var             endsWith   = false;

            foreach (var child in node.Children)
            {
                switch (child)
                {
                case IInternalTreeNode internalNode:
                    if (internalNode.Is(RegexGrammar.Expression))
                    {
                        expression = VisitRegexExpressionNode(internalNode);
                    }

                    break;

                case ITokenTreeNode tokenNode:
                    switch (tokenNode.Token.Value)
                    {
                    case "$":
                        endsWith = true;
                        break;

                    case "^":
                        startsWith = true;
                        break;
                    }

                    break;
                }
            }

            return(new Regex(startsWith, expression, endsWith));
        }
Ejemplo n.º 8
0
 public RegexExpressionAlteration(RegexTerm term, RegexExpression expression)
     : base(term)
 {
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }
Ejemplo n.º 9
0
 public Regex(bool startsWith, RegexExpression expression, bool endsWith)
 {
     StartsWith = startsWith;
     EndsWith   = endsWith;
     Expression = expression;
 }
Ejemplo n.º 10
0
 public RegexAtomExpression(RegexExpression expression)
 {
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }