Beispiel #1
0
 public RegexDefinition(
     bool startsWith,
     RegexExpression expression,
     bool endsWith)
 {
     StartsWith = startsWith;
     EndsWith   = endsWith;
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }
        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");
        }
Beispiel #3
0
        private RegexDefinition 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.Capture[0])
                    {
                    case '$':
                        endsWith = true;
                        break;

                    case '^':
                        startsWith = true;
                        break;
                    }
                    break;
                }
            }
            return(new RegexDefinition(startsWith, expression, endsWith));
        }
Beispiel #4
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.");
        }
Beispiel #5
0
 public RegexAtomExpression(RegexExpression expression)
 {
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }
Beispiel #6
0
 public RegexExpressionAlteration(RegexTerm term, RegexExpression expression)
     : base(term)
 {
     Expression = expression;
     _hashCode  = ComputeHashCode();
 }