Exemplo n.º 1
0
 protected BasePattern CreateGroupOrSingleton(bool isCapturing, BasePattern[] children)
 {
     if (!isCapturing && children.Length == 1)
         return children[0];
     else
         return new GroupPattern(isCapturing, children);
 }
        public override BasePattern Transform(BasePattern pattern)
        {
            BasePattern transformed = pattern;

            if (pattern.Type == PatternType.Char)
            {
                if (Options.Singleline && transformed is AnyCharPattern)
                    transformed = new AnyCharPattern(true);

                if (Options.IgnoreCase)
                    transformed = ((CharPattern)transformed).CaseInsensitive;
            }
            else if (pattern.Type == PatternType.Anchor)
            {
                AnchorPattern anchor = (AnchorPattern)pattern;

                if (anchor.AnchorType == AnchorType.StartOfStringOrLine)
                    transformed = new AnchorPattern(Options.Multiline ? AnchorType.StartOfLine : AnchorType.StartOfString);
                else if (anchor.AnchorType == AnchorType.EndOfStringOrLine)
                    transformed = new AnchorPattern(Options.Multiline ? AnchorType.EndOfLine : AnchorType.EndOfStringOrBeforeEndingNewline);
            }

            if (transformed != pattern)
                return transformed;
            else
                return base.Transform(pattern);
        }
Exemplo n.º 3
0
        public virtual BasePattern Transform(BasePattern pattern)
        {
            // The Identity transform

            switch (pattern.Type)
            {
                case PatternType.Group:
                    GroupPattern group = (GroupPattern)pattern;

                    BasePattern[] newChildren = group.Patterns
                                                     .Select(p => Transform(p))
                                                     .Where(IsNotEmpty)
                                                     .ToArray();

                    return CreateGroupOrSingleton(group.IsCapturing, newChildren);

                case PatternType.Quantifier:
                    QuantifierPattern quant = (QuantifierPattern)pattern;
                    BasePattern newChild = Transform(quant.ChildPattern);

                    if (IsNotEmpty(newChild))
                        return new QuantifierPattern(newChild, quant.MinOccurrences, quant.MaxOccurrences, quant.IsGreedy);
                    else
                        return GroupPattern.Empty;

                case PatternType.Alternation:
                    return new AlternationPattern(((AlternationPattern)pattern).Alternatives
                                                                               .Select(a => Transform(a)));

                default:
                    return pattern;
            }
        }
        public override BasePattern Transform(BasePattern pattern)
        {
            if (pattern.Type == PatternType.Quantifier)
            {
                QuantifierPattern quant = (QuantifierPattern)pattern;
                BasePattern transformedChild = Transform(quant.ChildPattern);

                if (IsEmpty(transformedChild))
                    return GroupPattern.Empty;

                BasePattern[] newPatterns = new[]
                    {
                        new QuantifierPattern(transformedChild,
                                              quant.MinOccurrences,
                                              quant.MinOccurrences,
                                              quant.IsGreedy),
                        new QuantifierPattern(transformedChild,
                                              0,
                                              quant.MaxOccurrences != null ? quant.MaxOccurrences - quant.MinOccurrences : null,
                                              quant.IsGreedy),
                    }
                    .Select(q => reduceQuantifier(q))
                    .Where(IsNotEmpty)
                    .ToArray();

                if (newPatterns.Length == 1)
                    return newPatterns[0];
                else
                    return new GroupPattern(false, newPatterns);
            }
            else
                return base.Transform(pattern);
        }
Exemplo n.º 5
0
        public override BasePattern Transform(BasePattern pattern)
        {
            if (pattern.Type == PatternType.Group)
            {
                // TODO: use groupBy (Haskell-style)

                GroupPattern group = (GroupPattern)pattern;
                List<BasePattern> newChildPatterns = new List<BasePattern>();
                StringBuilder currentString = new StringBuilder();

                foreach (BasePattern oldChildPattern in group.Patterns)
                    if (oldChildPattern is CharEscapePattern)
                        currentString.Append(((CharEscapePattern)oldChildPattern).Value);
                    else
                    {
                        if (currentString.Length > 0)
                        {
                            addStringPattern(newChildPatterns, currentString.ToString());
                            currentString = new StringBuilder();
                        }

                        newChildPatterns.Add(Transform(oldChildPattern));
                    }

                if (currentString.Length > 0)
                    addStringPattern(newChildPatterns, currentString.ToString());

                return CreateGroupOrSingleton(group.IsCapturing, newChildPatterns.ToArray());
            }
            else
                return base.Transform(pattern);
        }
Exemplo n.º 6
0
        public QuantifierPattern(BasePattern childPattern, int minOccurrences, int?maxOccurrences, bool isGreedy)
            : base(PatternType.Quantifier, minOccurrences * childPattern.MinCharLength)
        {
            if (childPattern == null)
            {
                throw new ArgumentNullException("childPattern.", "Child pattern is null in quantifier pattern.");
            }

            minOccurrences = Math.Max(0, minOccurrences);

            if (maxOccurrences != null)
            {
                maxOccurrences = Math.Max(0, (int)maxOccurrences);

                if (minOccurrences > maxOccurrences)
                {
                    throw new ArgumentException(
                              "Quantifier pattern: the maximum number of occurrences must be greater than or equal to the minimum number.",
                              "maxOccurrences.");
                }
            }

            ChildPattern   = childPattern;
            MinOccurrences = minOccurrences;
            MaxOccurrences = maxOccurrences;
            IsGreedy       = isGreedy;
        }
Exemplo n.º 7
0
        private Parser<char, string> createParser(BasePattern pattern)
        {
            if (pattern == null)
                throw new ArgumentNullException("pattern.", "Pattern is null when creating match parser.");

            switch (pattern.Type)
            {
                case PatternType.Group:
                    return from vs in
                               CharParsers.Sequence(((GroupPattern)pattern).Patterns
                                                                           .Select(p => createParser(p)))
                           select vs.JoinStrings();

                case PatternType.Quantifier:
                    QuantifierPattern quant = (QuantifierPattern)pattern;
                    return from vs in CharParsers.Count(quant.MinOccurrences,
                                                        quant.MaxOccurrences,
                                                        createParser(quant.ChildPattern))
                           select vs.JoinStrings();

                case PatternType.Alternation:
                    return CharParsers.Choice(((AlternationPattern)pattern).Alternatives
                                                                           .Select(p => createParser(p))
                                                                           .ToArray());

                case PatternType.String:
                    return CharParsers.String(((StringPattern)pattern).Value);

                case PatternType.Char:
                    return from c in CharParsers.Satisfy(((CharPattern)pattern).IsMatch)
                           select new string(c, 1);

                default:
                    throw new ApplicationException(
                        string.Format("ExplicitDFAMatcher: unrecognized pattern type ({0}).",
                                      pattern.GetType().Name));
            }
        }
Exemplo n.º 8
0
 protected bool IsNotEmpty(BasePattern pattern)
 {
     return !IsEmpty(pattern);
 }
Exemplo n.º 9
0
 protected bool IsEmpty(BasePattern pattern)
 {
     return pattern.Type == PatternType.Group && pattern.Equals(GroupPattern.Empty);
 }
Exemplo n.º 10
0
        protected override BasePattern TransformAST(BasePattern pattern)
        {
            pattern = base.TransformAST(pattern);

            return new StringASTTransform().Transform(pattern);
        }
Exemplo n.º 11
0
 protected virtual BasePattern TransformAST(BasePattern pattern)
 {
     return new RegexOptionsASTTransform(Options).Transform(pattern);
 }
Exemplo n.º 12
0
        protected override BasePattern TransformAST(BasePattern pattern)
        {
            pattern = base.TransformAST(pattern);

            return new QuantifierASTTransform().Transform(pattern);
        }
Exemplo n.º 13
0
 private static BasePattern doTransform(BasePattern pattern, BaseASTTransform transform)
 {
     return transform.Transform(pattern);
 }
Exemplo n.º 14
0
        private static void displayASTTransform(string patternText, BasePattern beforePattern, BasePattern afterPattern)
        {
            Console.WriteLine("Pattern Text:\n    {0}", patternText.ShowVerbatim());
            Console.WriteLine("Before Transform:");
            Console.WriteLine(beforePattern.FormatAsTree(1));
            Console.WriteLine("After Transform{0}:", afterPattern.Equals(beforePattern) ? " (unchanged)" : "");
            Console.WriteLine(afterPattern.FormatAsTree(1));

            Console.Write("\n");
        }
Exemplo n.º 15
0
        public static void IsASTTransformCorrect(BasePattern expected, string patternText, AlgorithmType algorithmType, RegexOptions options)
        {
            BasePattern beforePattern = BasePattern.CreatePattern(patternText);
            BasePattern afterPattern = doTransform(patternText, algorithmType, options);

            displayASTTransform(patternText, beforePattern, afterPattern);

            Assert.AreEqual(expected, afterPattern);
        }
Exemplo n.º 16
0
        public static void IsASTTransformCorrect(BasePattern expected, string patternText, BaseASTTransform transform)
        {
            BasePattern beforePattern = BasePattern.CreatePattern(patternText);
            BasePattern afterPattern = doTransform(beforePattern, transform);

            displayASTTransform(patternText, beforePattern, afterPattern);

            Assert.AreEqual(expected, afterPattern);
        }