Beispiel #1
0
 private void FindLeaves(
     RegExp exp,
     Kind regExpKind,
     IList <Automaton> list,
     IDictionary <String, Automaton> automata,
     IAutomatonProvider automatonProvider,
     bool minimize)
 {
     if (exp.kind == regExpKind)
     {
         this.FindLeaves(exp.exp1, regExpKind, list, automata, automatonProvider, minimize);
         this.FindLeaves(exp.exp2, regExpKind, list, automata, automatonProvider, minimize);
     }
     else
     {
         list.Add(exp.ToAutomaton(automata, automatonProvider, minimize));
     }
 }
Beispiel #2
0
        private Automaton ToAutomatonAllowMutate(
            IDictionary <string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            bool @bool = false;

            if (allowMutation)
            {
                @bool = RegExp.SetAllowMutate(true); // This is not thead safe.
            }

            Automaton a = this.ToAutomaton(automata, automatonProvider, minimize);

            if (allowMutation)
            {
                RegExp.SetAllowMutate(@bool);
            }

            return(a);
        }
Beispiel #3
0
 private void FindLeaves(
     RegExp exp,
     Kind regExpKind,
     IList<Automaton> list,
     IDictionary<String, Automaton> automata,
     IAutomatonProvider automatonProvider,
     bool minimize)
 {
     if (exp.kind == regExpKind)
     {
         this.FindLeaves(exp.exp1, regExpKind, list, automata, automatonProvider, minimize);
         this.FindLeaves(exp.exp2, regExpKind, list, automata, automatonProvider, minimize);
     }
     else
     {
         list.Add(exp.ToAutomaton(automata, automatonProvider, minimize));
     }
 }
Beispiel #4
0
 /// <summary>
 ///   Constructs new <code>Automaton</code> from this <code>RegExp</code>. 
 ///   The constructed automaton has no transitions to dead states.
 /// </summary>
 /// <param name = "automatonProvider">The provider of automata for named identifiers.</param>
 /// <param name = "minimize">if set to <c>true</c> the automaton is minimized and determinized.</param>
 /// <returns></returns>
 public Automaton ToAutomaton(IAutomatonProvider automatonProvider, bool minimize)
 {
     return this.ToAutomatonAllowMutate(null, automatonProvider, minimize);
 }
Beispiel #5
0
 /// <summary>
 ///   Constructs new <code>Automaton</code> from this <code>RegExp</code>. 
 ///   The constructed automaton is minimal and deterministic and has no 
 ///   transitions to dead states.
 /// </summary>
 /// <param name = "automatonProvider">The provider of automata for named identifiers.</param>
 /// <returns></returns>
 public Automaton ToAutomaton(IAutomatonProvider automatonProvider)
 {
     return this.ToAutomatonAllowMutate(null, automatonProvider, true);
 }
Beispiel #6
0
        private Automaton ToAutomaton(
            IDictionary <string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            IList <Automaton> list;
            Automaton         a = null;

            switch (this.kind)
            {
            case Kind.RegexpUnion:
                list = new List <Automaton>();
                this.FindLeaves(this.exp1, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                this.FindLeaves(this.exp2, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                a = BasicOperations.Union(list);
                a.Minimize();
                break;

            case Kind.RegexpConcatenation:
                list = new List <Automaton>();
                this.FindLeaves(this.exp1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                this.FindLeaves(this.exp2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                a = BasicOperations.Concatenate(list);
                a.Minimize();
                break;

            case Kind.RegexpIntersection:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize)
                    .Intersection(this.exp2.ToAutomaton(automata, automatonProvider, minimize));
                a.Minimize();
                break;

            case Kind.RegexpOptional:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize).Optional();
                a.Minimize();
                break;

            case Kind.RegexpRepeat:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat();
                a.Minimize();
                break;

            case Kind.RegexpRepeatMin:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(this.min);
                a.Minimize();
                break;

            case Kind.RegexpRepeatMinMax:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(this.min, this.max);
                a.Minimize();
                break;

            case Kind.RegexpComplement:
                a = this.exp1.ToAutomaton(automata, automatonProvider, minimize).Complement();
                a.Minimize();
                break;

            case Kind.RegexpChar:
                a = BasicAutomata.MakeChar(this.c);
                break;

            case Kind.RegexpCharRange:
                a = BasicAutomata.MakeCharRange(this.@from, this.to);
                break;

            case Kind.RegexpAnyChar:
                a = BasicAutomata.MakeAnyChar();
                break;

            case Kind.RegexpEmpty:
                a = BasicAutomata.MakeEmpty();
                break;

            case Kind.RegexpString:
                a = BasicAutomata.MakeString(this.s);
                break;

            case Kind.RegexpAnyString:
                a = BasicAutomata.MakeAnyString();
                break;

            case Kind.RegexpAutomaton:
                Automaton aa = null;
                if (automata != null)
                {
                    automata.TryGetValue(this.s, out aa);
                }

                if (aa == null && automatonProvider != null)
                {
                    try
                    {
                        aa = automatonProvider.GetAutomaton(this.s);
                    }
                    catch (IOException e)
                    {
                        throw new ArgumentException(string.Empty, e);
                    }
                }

                if (aa == null)
                {
                    throw new ArgumentException("'" + this.s + "' not found");
                }

                a = aa.Clone();     // Always clone here (ignore allowMutate).
                break;

            case Kind.RegexpInterval:
                a = BasicAutomata.MakeInterval(this.min, this.max, this.digits);
                break;
            }

            return(a);
        }
Beispiel #7
0
 internal Automaton ToAutomaton(IAutomatonProvider automatonProvider)
 {
     return(this.ToAutomatonAllowMutate(null, automatonProvider, true));
 }
Beispiel #8
0
 public Automaton ToAutomaton(IAutomatonProvider automatonProvider, bool minimize) => ToAutomatonAllowMutate(null, automatonProvider, minimize);
Beispiel #9
0
 private void FindLeaves(RegExp exp, Kind kind, IList <Automaton> list, IDictionary <string, Automaton> automata, IAutomatonProvider automaton_provider)
 {
     if (exp.kind == kind)
     {
         FindLeaves(exp.exp1, kind, list, automata, automaton_provider);
         FindLeaves(exp.exp2, kind, list, automata, automaton_provider);
     }
     else
     {
         list.Add(exp.ToAutomaton(automata, automaton_provider));
     }
 }
Beispiel #10
0
 /// <summary>
 /// Constructs a query for terms matching <paramref name="term"/>.
 /// </summary>
 /// <param name="term"> Regular expression. </param>
 /// <param name="flags"> Optional <see cref="RegExp"/> features from <see cref="RegExpSyntax"/> </param>
 /// <param name="provider"> Custom <see cref="IAutomatonProvider"/> for named automata </param>
 public RegexpQuery(Term term, RegExpSyntax flags, IAutomatonProvider provider)
     : base(term, (new RegExp(term.Text(), flags)).ToAutomaton(provider))
 {
 }
Beispiel #11
0
        private Automaton ToAutomaton(IDictionary <string, Automaton> automata, IAutomatonProvider automaton_provider)
        {
            IList <Automaton> list;
            Automaton         a = null;

            switch (kind)
            {
            case Kind.REGEXP_UNION:
                list = new List <Automaton>();
                FindLeaves(exp1, Kind.REGEXP_UNION, list, automata, automaton_provider);
                FindLeaves(exp2, Kind.REGEXP_UNION, list, automata, automaton_provider);
                a = BasicOperations.Union(list);
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_CONCATENATION:
                list = new List <Automaton>();
                FindLeaves(exp1, Kind.REGEXP_CONCATENATION, list, automata, automaton_provider);
                FindLeaves(exp2, Kind.REGEXP_CONCATENATION, list, automata, automaton_provider);
                a = BasicOperations.Concatenate(list);
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_INTERSECTION:
                a = exp1.ToAutomaton(automata, automaton_provider).Intersection(exp2.ToAutomaton(automata, automaton_provider));
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_OPTIONAL:
                a = exp1.ToAutomaton(automata, automaton_provider).Optional();
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_REPEAT:
                a = exp1.ToAutomaton(automata, automaton_provider).Repeat();
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_REPEAT_MIN:
                a = exp1.ToAutomaton(automata, automaton_provider).Repeat(min);
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_REPEAT_MINMAX:
                a = exp1.ToAutomaton(automata, automaton_provider).Repeat(min, max);
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_COMPLEMENT:
                a = exp1.ToAutomaton(automata, automaton_provider).Complement();
                MinimizationOperations.Minimize(a);
                break;

            case Kind.REGEXP_CHAR:
                a = BasicAutomata.MakeChar(c);
                break;

            case Kind.REGEXP_CHAR_RANGE:
                a = BasicAutomata.MakeCharRange(from, to);
                break;

            case Kind.REGEXP_ANYCHAR:
                a = BasicAutomata.MakeAnyChar();
                break;

            case Kind.REGEXP_EMPTY:
                a = BasicAutomata.MakeEmpty();
                break;

            case Kind.REGEXP_STRING:
                a = BasicAutomata.MakeString(s);
                break;

            case Kind.REGEXP_ANYSTRING:
                a = BasicAutomata.MakeAnyString();
                break;

            case Kind.REGEXP_AUTOMATON:
                Automaton aa = null;
                if (automata != null)
                {
                    aa = automata[s];
                }
                if (aa == null && automaton_provider != null)
                {
                    try
                    {
                        aa = automaton_provider.GetAutomaton(s);
                    }
                    catch (Exception e) when(e.IsIOException())
                    {
                        throw new ArgumentException(e.ToString(), e);
                    }
                }
                if (aa == null)
                {
                    throw new ArgumentException("'" + s + "' not found");
                }
                a = (Automaton)aa.Clone();     // always clone here (ignore allow_mutate)
                break;

            case Kind.REGEXP_INTERVAL:
                a = BasicAutomata.MakeInterval(min, max, digits);
                break;
            }
            return(a);
        }
Beispiel #12
0
        private Automaton ToAutomatonAllowMutate(IDictionary <string, Automaton> automata, IAutomatonProvider automaton_provider)
        {
            bool b = false;

            if (allow_mutation) // thread unsafe
            {
                b = Automaton.SetAllowMutate(true);
            }
            Automaton a = ToAutomaton(automata, automaton_provider);

            if (allow_mutation)
            {
                Automaton.SetAllowMutate(b);
            }
            return(a);
        }
Beispiel #13
0
 public virtual Automaton ToAutomaton(IAutomatonProvider automaton_provider)
 {
     return(ToAutomatonAllowMutate(null, automaton_provider));
 }
Beispiel #14
0
        private Automaton ToAutomaton(
            IDictionary<string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            IList<Automaton> list;
            Automaton a = null;
            switch (kind)
            {
                case Kind.RegexpUnion:
                    list = new List<Automaton>();
                    this.FindLeaves(exp1, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                    this.FindLeaves(exp2, Kind.RegexpUnion, list, automata, automatonProvider, minimize);
                    a = BasicOperations.Union(list);
                    a.Minimize();
                    break;
                case Kind.RegexpConcatenation:
                    list = new List<Automaton>();
                    this.FindLeaves(exp1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                    this.FindLeaves(exp2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize);
                    a = BasicOperations.Concatenate(list);
                    a.Minimize();
                    break;
                case Kind.RegexpIntersection:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize)
                        .Intersection(exp2.ToAutomaton(automata, automatonProvider, minimize));
                    a.Minimize();
                    break;
                case Kind.RegexpOptional:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize).Optional();
                    a.Minimize();
                    break;
                case Kind.RegexpRepeat:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat();
                    a.Minimize();
                    break;
                case Kind.RegexpRepeatMin:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min);
                    a.Minimize();
                    break;
                case Kind.RegexpRepeatMinMax:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min, max);
                    a.Minimize();
                    break;
                case Kind.RegexpComplement:
                    a = exp1.ToAutomaton(automata, automatonProvider, minimize).Complement();
                    a.Minimize();
                    break;
                case Kind.RegexpChar:
                    a = BasicAutomata.MakeChar(c);
                    break;
                case Kind.RegexpCharRange:
                    a = BasicAutomata.MakeCharRange(from, to);
                    break;
                case Kind.RegexpAnyChar:
                    a = BasicAutomata.MakeAnyChar();
                    break;
                case Kind.RegexpEmpty:
                    a = BasicAutomata.MakeEmpty();
                    break;
                case Kind.RegexpString:
                    a = BasicAutomata.MakeString(s);
                    break;
                case Kind.RegexpAnyString:
                    a = BasicAutomata.MakeAnyString();
                    break;
                case Kind.RegexpAutomaton:
                    Automaton aa = null;
                    if (automata != null)
                    {
                        automata.TryGetValue(s, out aa);
                    }

                    if (aa == null && automatonProvider != null)
                    {
                        try
                        {
                            aa = automatonProvider.GetAutomaton(s);
                        }
                        catch (IOException e)
                        {
                            throw new ArgumentException(string.Empty, e);
                        }
                    }

                    if (aa == null)
                    {
                        throw new ArgumentException("'" + s + "' not found");
                    }

                    a = aa.Clone(); // Always clone here (ignore allowMutate).
                    break;
                case Kind.RegexpInterval:
                    a = BasicAutomata.MakeInterval(min, max, digits);
                    break;
            }

            return a;
        }
Beispiel #15
0
 internal Automaton ToAutomaton(IAutomatonProvider automatonProvider, bool minimize)
 {
     return(this.ToAutomatonAllowMutate(null, automatonProvider, minimize));
 }
Beispiel #16
0
        private Automaton ToAutomatonAllowMutate(
            IDictionary<string, Automaton> automata,
            IAutomatonProvider automatonProvider,
            bool minimize)
        {
            bool @bool = false;
            if (allowMutation)
            {
                @bool = this.SetAllowMutate(true); // This is not thead safe.
            }

            Automaton a = this.ToAutomaton(automata, automatonProvider, minimize);
            if (allowMutation)
            {
                this.SetAllowMutate(@bool);
            }

            return a;
        }
Beispiel #17
0
 public Automaton ToAutomaton(IAutomatonProvider automatonProvider) => ToAutomatonAllowMutate(null, automatonProvider, true);