/// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (parent is NotTestAST)
     {
         if (IsExtended)
             sb.AppendFormat("\\P{{scx={0}}}", Script.Abbreviation());
         else
             sb.AppendFormat("\\P{{sc={0}}}", Script.Abbreviation());
     }
     else
     {
         if (IsExtended)
             sb.AppendFormat("\\p{{scx={0}}}", Script.Abbreviation());
         else
             sb.AppendFormat("\\p{{sc={0}}}", Script.Abbreviation());
     }
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (parent is NotTestAST)
         sb.Append("\\P{");
     else
         sb.Append("\\p{");
     sb.Append(Category.Abbreviation());
     sb.Append("}");
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     sb.Append(Property.Abbreviation(!(parent is NotTestAST)));
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(StringBuilder sb, RegexAST parent)
 {
     Argument.Write(sb, this);
 }
 /// <summary>Indicates whether the instruction is generated from a regex as part of a derivative.</summary>
 /// <param name="ast">A regex from which the instruction may have been generated.</param>
 /// <returns><c>true</c> if <paramref name="ast"/> is the origin of this instruction, otherwise <c>false</c>.</returns>
 public virtual bool ComesFrom(RegexAST ast)
 {
     return false;
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(StringBuilder sb, RegexAST parent)
 {
 }
        private static RegexAST Quantifier(string text, RegexOptions opts, ref int pos, RegexAST baseAst)
        {
            bool greedy = true;
            int min = -1, max = -1;
            if (pos >= text.Length)
                return baseAst;
            else switch (text[pos])
                {
                    case '?': pos++; min = 0; max = 1; break;
                    case '*': pos++; min = 0; break;
                    case '+': pos++; min = 1; break;

                    case '{':
                        pos++;
                        if (pos >= text.Length)
                            throw new RegexParseException(text, pos, "unterminated quantifier");

                        min = ReadNumber(text, ref pos);
                        if (pos >= text.Length)
                            throw new RegexParseException(text, pos, "unterminated quantifier");
                        else if (text[pos] == '}')
                        {
                            pos++;
                            max = min;
                        }
                        else if (text[pos] == ',')
                        {
                            pos++;
                            if (pos >= text.Length)
                                throw new RegexParseException(text, pos, "unterminated quantifier");
                            else if (text[pos] != '}')
                            {
                                max = ReadNumber(text, ref pos);
                                if (pos >= text.Length || text[pos] != '}')
                                    throw new RegexParseException(text, pos, "unterminated quantifier");
                            }
                            pos++;
                        }
                        else
                            throw new RegexParseException(text, pos, "unterminated quantifier");
                        break;
                }
            if (min != max)
            {
                greedy = !(pos < text.Length && text[pos] == '?');
                if (!greedy) pos++;
            }

            if (min < 0)
                return baseAst;
            else if (max >= 0 && max < min)
                throw new RegexParseException(text, pos, "max repeats less than min repeats");
            else
                return Quantifier(text, opts, ref pos, QuantifierAST.Make(baseAst, min, max, greedy));
        }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     sb.AppendFormat("\\g{0:d}", Source.CaptureID);
     sb.Append("{0}");
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (FoldsCase)
         sb.Append("(?i)");
     sb.Append("\\");
     if (Name == null || CaptureID.ToString() == Name)
     {
         sb.Append(CaptureID.ToString());
     }
     else
     {
         sb.Append("m{");
         sb.Append(Name);
         sb.Append("}");
     }
 }
 /// <summary>Initializes and compiles a regular expression, with options that modify the pattern.</summary>
 /// <param name="regexRep">The pattern to be compiled.</param>
 /// <param name="opts">The options desired.</param>
 /// <exception cref="ArgumentException"><paramref name="regexRep"/> is an ill-formed pattern.</exception>
 internal NFABuilder(string regexRep, RegexOptions opts)
 {
     pending = new Queue<ASTTransition>();
     visited = new Dictionary<RegexAST, int>();
     instructIndex = ast =>
     {
         Instructions.Add(ast.ToInstruction(this));
         return Instructions.Count - 1;
     };
     instructIndex = instructIndex.Memoize();
     ParseInfo = new AnnotationVisitor(regexRep);
     SyntaxTree = Parser.Parse(regexRep, opts);
     SyntaxTree.Annotate(ParseInfo);
     IgnoreCase = (opts & RegexOptions.IgnoreCase) != 0;
 }
        public override bool ComesFrom(RegexAST ast)
        {
            StarAST starAST = ast as StarAST;
            if (starAST != null)
                return ComesFrom(starAST.Argument);

            QuantifierAST quantAST = ast as QuantifierAST;
            if (quantAST != null)
                return ComesFrom(quantAST.Argument);

            return Source.Equals(ast);
        }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (FoldsCase)
         sb.Append("(?i)");
     string rangeName = String.Format("{0}-{1}", Parser.EscapeChar(Min, true), Parser.EscapeChar(Max, true));
     if (parent is NotTestAST)
         sb.AppendFormat("[^{0}]", rangeName);
     else if (parent is OrTestAST)
         sb.AppendFormat(rangeName);
     else
         sb.AppendFormat("[{0}]", rangeName);
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (FoldsCase)
         sb.Append("(?i)");
     if (parent is NotTestAST)
         sb.AppendFormat("[^{0}]", Parser.EscapeChar(Character, true));
     else if (parent is OrTestAST)
         sb.Append(Parser.EscapeChar(Character, true));
     else
         sb.Append(Parser.EscapeChar(Character, false));
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(StringBuilder sb, RegexAST parent)
 {
     sb.Append((parent is NotTestAST) ? "\\P{Any}" : "\\p{Any}");
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(StringBuilder sb, RegexAST parent)
 {
     bool parens = !(parent is OrTestAST || parent is AndTestAST);
     if (parens)
     {
         sb.Append("[");
         if (parent is NotTestAST) sb.Append("^");
     }
     Left.Write(sb, this);
     sb.Append("&&");
     Right.Write(sb, this);
     if (parens) sb.Append("]");
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     if (Source.FoldsCase)
         sb.Append("(?i)");
     sb.AppendFormat("\\g{0:d}", Source.CaptureID);
 }
 /// <summary>Indicates whether the start of the instruction was generated from a regex as part of a derivative.</summary>
 /// <param name="ast">A regex from which <see cref="Left"/> may have been generated.</param>
 /// <returns><c>true</c> if <paramref name="ast"/> is the origin of <see cref="Left"/>, otherwise <c>false</c>.</returns>
 public override bool ComesFrom(RegexAST ast)
 {
     return Left.ComesFrom(ast);
 }
 /// <summary>Flattens the syntax tree into a representation of the source regex.</summary>
 /// <param name="sb">Buffer for the output string.</param>
 /// <param name="parent">The tree, if any, containing this syntax tree.</param>
 public override void Write(System.Text.StringBuilder sb, RegexAST parent)
 {
     sb.Append((parent is NotTestAST) ? "\\C{" : "\\c{");
     sb.Append(Index);
     sb.Append("}");
 }