Beispiel #1
0
        public override string Generate(Random random)
        {
            if (this == RegexCompiler.InvalidNode)
            {
                //select a character
                int pos = random.Next(mNodeText.Length);

                //generate any other character using a negative SetNode
                RegexSetNode others = new RegexSetNode(false);
                others.AddChars(mNodeText[pos].ToString());

                //replace the character
                char backup = mNodeText[pos];
                mNodeText[pos] = others.Generate(random)[0];
                string result = mNodeText.ToString();

                //if this node is repeated it needs to be cleaned up for the next call
                mNodeText[pos] = backup;

                return(result);
            }
            else
            {
                return(mNodeText.ToString());
            }
        }
Beispiel #2
0
        //Compile \d \D \s \S etc.
        public RegexNode CompileSimpleMacro(char c)
        {
            RegexNode    node = null;
            RegexSetNode set  = null;

            if (@"[]{}()*-+.?\|".Contains(c.ToString()))
            {
                return(new RegexTextNode(c.ToString()));
            }

            switch (c)
            {
            case 'd':     // [0-9]
                node = set = new RegexSetNode(true);
                set.AddRange('0', '9');
                break;

            case 'D':     // [^0-9]
                node = set = new RegexSetNode(false);
                set.AddRange('0', '9');
                break;

            case 's':
                node = set = new RegexSetNode(true);
                set.AddChars(" \r\n\f\v\t");
                break;

            case 'S':
                node = set = new RegexSetNode(false);
                set.AddChars(" \r\n\f\v\t");
                break;

            case 'w':     // [a-zA-Z0-9_]
                node = set = new RegexSetNode(true);
                set.AddRange('a', 'z');
                set.AddRange('A', 'Z');
                set.AddRange('0', '9');
                set.AddChars("_");
                break;

            case 'W':     // [^a-zA-Z0-9_]
                node = set = new RegexSetNode(false);
                set.AddRange('a', 'z');
                set.AddRange('A', 'Z');
                set.AddRange('0', '9');
                set.AddChars("_");
                break;

            case 'f':
                node = new RegexTextNode("\f");
                break;

            case 'n':
                node = new RegexTextNode("\n");
                break;

            case 'r':
                node = new RegexTextNode("\r");
                break;

            case 't':
                node = new RegexTextNode("\t");
                break;

            case 'v':
                node = new RegexTextNode("\v");
                break;

            case 'A':
            case 'Z':
            case 'z':
                node = new RegexTextNode(String.Empty);
                break;

            default:
                AssertParse(false, "Invalid escape.");
                break;
            }

            return(node);
        }