コード例 #1
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;
            }
        }
コード例 #2
0
 protected BasePattern CreateGroupOrSingleton(bool isCapturing, BasePattern[] children)
 {
     if (!isCapturing && children.Length == 1)
         return children[0];
     else
         return new GroupPattern(isCapturing, children);
 }
コード例 #3
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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
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));
            }
        }
コード例 #7
0
 protected bool IsNotEmpty(BasePattern pattern)
 {
     return !IsEmpty(pattern);
 }
コード例 #8
0
 protected bool IsEmpty(BasePattern pattern)
 {
     return pattern.Type == PatternType.Group && pattern.Equals(GroupPattern.Empty);
 }
コード例 #9
0
        protected override BasePattern TransformAST(BasePattern pattern)
        {
            pattern = base.TransformAST(pattern);

            return new StringASTTransform().Transform(pattern);
        }
コード例 #10
0
ファイル: BaseMatcher.cs プロジェクト: aistrate/RegexParser
 protected virtual BasePattern TransformAST(BasePattern pattern)
 {
     return new RegexOptionsASTTransform(Options).Transform(pattern);
 }
コード例 #11
0
        protected override BasePattern TransformAST(BasePattern pattern)
        {
            pattern = base.TransformAST(pattern);

            return new QuantifierASTTransform().Transform(pattern);
        }
コード例 #12
0
ファイル: RegexAssert.cs プロジェクト: aistrate/RegexParser
 private static BasePattern doTransform(BasePattern pattern, BaseASTTransform transform)
 {
     return transform.Transform(pattern);
 }
コード例 #13
0
ファイル: RegexAssert.cs プロジェクト: aistrate/RegexParser
        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");
        }
コード例 #14
0
ファイル: RegexAssert.cs プロジェクト: aistrate/RegexParser
        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);
        }
コード例 #15
0
ファイル: RegexAssert.cs プロジェクト: aistrate/RegexParser
        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);
        }