public static ITranslationObject[] LoadFromJSON(string path)
    {
        var TranslationObjects = new List <ITranslationObject>();

        Dictionary <string, object>[] Dictionaries = Deserialize(path);

        foreach (var Dictionary in Dictionaries)
        {
            foreach (var Element in Dictionary)
            {
                if (Element.Key != "LOCALIZATION_COMMENT" || Element.Key != "LOCALIZATION_AUTHORS" || Element.Key != "LOCALIZATION_VERSION")
                {
                    if (Element.Key.StartsWith(RegexDetectionString))
                    {
                        string regexString = Element.Key.Remove(0, RegexDetectionString.Length);
                        var    RegexObject = new RegexObject(regexString, Element.Value.ToString());
                        TranslationObjects.Add(RegexObject);
                    }
                    else
                    {
                        var TranslationObject = new TranslationObject(Element.Key, Element.Value.ToString());
                        TranslationObjects.Add(TranslationObject);
                    }
                }
            }
        }
        return(TranslationObjects.ToArray());
    }
Beispiel #2
0
 public RegexBalanceGroup(RegexObject <T> regex, RegexObject <T> openItemInnerRegex, RegexObject <T> closeItemInnerRegex, object id = null) :
     this(
         regex,
         openItemInnerRegex,
         Enumerable.Empty <RegexObject <T> >(),
         closeItemInnerRegex,
         id
         )
 {
 }
 protected virtual IRegexFSMTransition <T> GenerateNFATransitionFromRegexObject(
     RegexObject <T> regex,
     IRegexNFA <T> nfa,
     IRegexNFAState <T> state
     )
 {
     if (regex is IRegexAnchorPoint <T> anchorPoint)
     {
         return(this.GenerateNFATransitionFromIRegexAnchorPoint(anchorPoint, nfa, state));
     }
     else if (regex is RegexGroup <T> group)
     {
         return(this.GenerateNFATransitionFromRegexGroup(group, nfa, state));
     }
     else if (regex is RegexGroupReference <T> groupReference)
     {
         return(this.GenerateNFATransitionFromRegexGroupReference(groupReference, nfa, state));
     }
     else if (regex is RegexMultiBranch <T> multiBranch)
     {
         return(this.GenerateNFATransitionFromRegexMultiBranch(multiBranch, nfa, state));
     }
     else if (regex is RegexCondition <T> condition)
     {
         return(this.GenerateNFATransitionFromRegexCondition(condition, nfa, state));
     }
     else if (regex is RegexRepeat <T> repeat)
     {
         return(this.GenerateNFATransitionFromRegexRepeat(repeat, nfa, state));
     }
     else if (regex is RegexNonGreedyRepeat <T> nonGreedyRepeat)
     {
         return(this.GenerateNFATransitionFromRegexNonGreedyRepeat(nonGreedyRepeat, nfa, state));
     }
     else if (regex is RegexSeries <T> series)
     {
         return(this.GenerateNFATransitionFromRegexSeries(series, nfa, state));
     }
     else if (regex is RegexParallels <T> parallels)
     {
         return(this.GenerateNFATransitionFromRegexParallels(parallels, nfa, state));
     }
     else
     {
         throw new NotSupportedException(string.Format("不支持的正则类型:{0}", regex.GetType()));
     }
 }
        public IRegexFSM <T> GenerateRegexFSMFromRegexObject(RegexObject <T> regex, RegexOptions options)
        {
            if (regex == null)
            {
                throw new ArgumentNullException(nameof(regex));
            }

            IRegexNFA <T>      nfa        = this.contextInfo.ActivateRegexNFA();
            IRegexNFAState <T> startState = this.contextInfo.ActivateRegexNFAState();

            nfa.StartState = startState;

            IRegexFSMTransition <T> transition = this.GenerateNFATransitionFromRegexObject(regex, nfa, startState);
            IRegexNFAState <T>      endState   = this.contextInfo.ActivateRegexNFAState(true);

            nfa.SetTarget(transition, endState);

            return(nfa);
        }
Beispiel #5
0
        public RegexBalanceGroup(RegexObject <T> regex, RegexObject <T> openItemInnerRegex, IEnumerable <RegexObject <T> > subItemInnerRegexs, RegexObject <T> closeItemInnerRegex, object id = null) : base(regex, id, true)
        {
            if (openItemInnerRegex == null)
            {
                throw new ArgumentNullException(nameof(openItemInnerRegex));
            }
            if (subItemInnerRegexs == null)
            {
                throw new ArgumentNullException(nameof(subItemInnerRegexs));
            }
            if (closeItemInnerRegex == null)
            {
                throw new ArgumentNullException(nameof(closeItemInnerRegex));
            }


            this.RegisterOpenItem(
                new RegexBalanceGroupOpenItem <T, int>(
                    openItemInnerRegex,
                    (() => this.subItems.Count)
                    )
                );
            foreach (var subItem in
                     subItemInnerRegexs.Select(subItemInnerRegex =>
                                               new RegexBalanceGroupSubItem <T, int>(
                                                   subItemInnerRegex,
                                                   (seed => seed--),
                                                   (seed => seed > 0)
                                                   )
                                               )
                     )
            {
                this.RegisterSubItem(subItem);
            }
            this.RegisterCloseItem(
                new RegexBalanceGroupCloseItem <T, int>(
                    closeItemInnerRegex,
                    (seed => -- seed == 0)
                    )
                );
        }
Beispiel #6
0
        public RegexBalanceGroup(RegexObject <T> regex, RegexBalanceGroupItem <T> openItem, IEnumerable <RegexBalanceGroupItem <T> > subItems, RegexBalanceGroupItem <T> closeItem, object id = null) : base(regex, id, true)
        {
            if (openItem == null)
            {
                throw new ArgumentNullException(nameof(openItem));
            }
            if (subItems == null)
            {
                throw new ArgumentNullException(nameof(subItems));
            }
            if (closeItem == null)
            {
                throw new ArgumentNullException(nameof(closeItem));
            }

            this.RegisterOpenItem(openItem);
            foreach (var subItem in subItems)
            {
                this.RegisterSubItem(subItem);
            }
            this.RegisterCloseItem(closeItem);
        }
        private static void TestFunctionalTransitions(RegexObject <char> regex)
        {
            RegexFAProvider <char> provider = new MyRegexFAProvider <char>(new RangeSetRegexStateMachineActivationContextInfo <char>(new RangeSet <char>(new[] { new CharRange() }, new CharRangeInfo())));
            IRegexFSM <char>       fsm;
            var nfa = provider.GenerateRegexFSMFromRegexObject(regex, RegexOptions.None);

            try
            {
                var dfa = provider.GenerateRegexDFAFromRegexFSM(nfa);
                fsm = dfa;
            }
            catch (Exception)
            {
                fsm = nfa;
            }
            ;

            var input = "1234564567";

            fsm.TransitMany(input);
            MatchCollection <char> matches = fsm.Matches;

            ;
        }
 public RegexGroup(RegexObject <T> regex, object id, bool isCaptive)
 {
     this.innerRegex = regex;
     this.id         = id;
     this.isCaptive  = isCaptive;
 }
 public RegexGroup(RegexObject <T> regex, bool isCaptive = true) : this(regex, null, isCaptive)
 {
 }
 public RegexBalanceGroupCloseItem(RegexObject <T> regex, Predicate <TSeed> method) : base(regex) =>
     this.method = method ?? throw new ArgumentNullException(nameof(method));
Beispiel #11
0
 public RegexMultiBranch(RegexObject <T> otherwisePattern) : this(Enumerable.Empty <(RegexMultiBranchBranchPredicate <T> predicate, RegexObject <T> pattern)>(), otherwisePattern)
 protected RegexBalanceGroupItem(RegexObject <T> regex) :
     base(regex ?? throw new ArgumentNullException(nameof(regex)), null, false)
 {
 }
 public RegexZeroLengthObject(RegexObject <T> regex) =>
 this.innerRegex = regex ?? throw new ArgumentNullException(nameof(regex));
 public RegexBalanceGroupSubItem(RegexObject <T> regex, Func <TSeed, TSeed> method, Predicate <TSeed> predicate = null) : base(regex)
 {
     this.method    = method ?? throw new ArgumentNullException(nameof(method));
     this.predicate = predicate;
 }
        static void Main(string[] args)
        {
            Program.TestFunctionalTransitions(
                Regex.Range('0', '9').NoneOrMany().NonGreedy() +
                Regex.Range('0', '9').Many().NonGreedy().Group("sec", true).GroupReference(out RegexGroupReference <char> sec_GroupReference) +
                sec_GroupReference
                );

#if false
            RangeSet <char> set = new RangeSet <char>(new CharRangeInfo());
            set.Add('a');
            set.Add('c');
            ;

            Dictionary <int, int> d = new Dictionary <int, int>();
            var chars  = Regex.Range('\0', 'z', true, false);
            var tchars = Regex.Range(new TT <char>('a'), new TT <char>('z'), false, true);

            var phone = Regex.Range(0, 9).RepeatMany(10);

            Func <int, RegexObject <char> > func = (count) =>
            {
                var junkPrefix             = Regex.Const('0').NoneOrMany();
                Func <int, char> convertor = Convert.ToChar;
                return(junkPrefix + Enumerable.Range(0, count).Select(num => num.ToString().Select(c => Regex.Const(c)).ConcatMany()).UnionMany());
            };
            var section = func(255);
            var dot = Regex.Const('.');
            var colon = Regex.Const(':');
            var port = func(9999);
            var ipAddress = new RegexObject <char>[] { section, dot, section, dot, section, dot, section, new RegexObject <char>[] { colon, port }.ConcatMany().Optional() }.ConcatMany();

            IRegexFAProvider <char> char_Provider = new RegexFAProvider <char>(new MyCharRegexRunContextInfo());

            var char_nfa = char_Provider.GenerateRegexFSMFromRegexObject(ipAddress, RegexOptions.None);
            //var debuginfo = char_nfa.GetDebugInfo();
            var char_dfa = char_Provider.GenerateRegexDFAFromRegexFSM(char_nfa);
            ;

            Action <RegexObject <char> > action =
                regexObj =>
            {
                var ___nfa = char_Provider.GenerateRegexFSMFromRegexObject(regexObj, RegexOptions.None);
                var ___dfa = char_Provider.GenerateRegexDFAFromRegexFSM(___nfa);

                IEnumerable <char> inputs       = Enumerable.Repeat <Func <int, int> >(new Random().Next, 25).Select(nextFunc => (char)('a' - 1 + nextFunc(4)));
                char[]             ___charArray = inputs.ToArray();
                IRegexFSM <char>   ___fsm;
#if false
                ___fsm = new RegexFSM <char>()
                {
                    StartState = ___dfa.StartState
                };
#else
                ___fsm = ___dfa;
#endif
                ___fsm.TransitMany(___charArray);

                var ___matches = ___fsm.Matches;
            };
            action?.Invoke(Regex.Const('a').Optional().Concat(Regex.Const('b').Concat(Regex.Const('c').Optional())));
#endif

            Func <int, int, RegexRange <string> > func_adpator = (min, max) =>
                                                                 new RegexRangeAdaptor <int, string>(
                min, max,
                (source => source.ToString()),
                (target => int.Parse(target))
                );
            var section_adpator = func_adpator(0, 255);
            var dot_adaptor     = new RegexConstAdaptor <char, string>('.', (source => source.ToString()), (target => target[0]));
            var colon_adaptor   = new RegexConstAdaptor <char, string>(':', (source => source.ToString()), (target => target[0]));
            var port_adaptor    = func_adpator(0, 9999);

            var ipAddress_adaptor =
                new RegexObject <string>[] { section_adpator, dot_adaptor, section_adpator, dot_adaptor, section_adpator, dot_adaptor, section_adpator, new RegexObject <string>[] { colon_adaptor, port_adaptor }.ConcatMany().Optional() }.ConcatMany();

            IRegexFAProvider <string> string_Provider = new RegexFAProvider <string>(new MyStringRegexRunContextInfo());

            var string_nfa = string_Provider.GenerateRegexFSMFromRegexObject(ipAddress_adaptor, RegexOptions.None);
            var string_dfa = string_Provider.GenerateRegexDFAFromRegexFSM(string_nfa);
            ;

            Random random = new Random();
            int    样本树    = 100;
            var    ipAddressStrFragments =
                Enumerable.Repeat(
                    new Tuple <Func <int>, Func <double> >(
                        (() => random.Next(4, 6)),
                        (() => random.NextDouble() * 1.15)
                        ),
                    样本树
                    )
                .Select(tuple =>
            {
                int groupCount = tuple.Item1();
                int sectionMax = 255;
                int portMax    = 9999;
                return
                (Enumerable.Repeat <IEnumerable <string> >(
                     new string[]
                {
                    ((int)(sectionMax * tuple.Item2())).ToString(),
                    "."
                },
                     3)
                 .Aggregate((ss1, ss2) => ss1.Concat(ss2))
                 .Concat(groupCount > 3 ?
                         new string[] { ((int)(sectionMax * tuple.Item2())).ToString() }
                         .Concat(groupCount > 4 ?
                                 new string[]
                {
                    ":",
                    ((int)(portMax * tuple.Item2())).ToString()
                } :
                                 Enumerable.Empty <string>()
                                 ) :
                         Enumerable.Empty <string>()
                         ));
            });
#if false
            IRegexFSM <char> char_fsm = char_dfa;
            var matchesArray          =
                ipAddressStrFragments
                .Select(ipAddressStrFragment => string.Join(string.Empty, ipAddressStrFragment))
                .Select(ipAddressStr =>
            {
                char_fsm.TransitMany(ipAddressStr);
                return(char_fsm.Matches);
            })
                .ToArray();
            ;
#else
            IRegexFSM <string> string_fsm = string_dfa;
            var matchesArray =
                ipAddressStrFragments
                .Select(ipAddressStr =>
            {
                string_fsm.TransitMany(ipAddressStr);
                return(string_fsm.Matches);
            })
                .ToArray();
            ;
#endif
        }
 public RegexMultiBranchPatternBranchPredicate(RegexObject <T> pattern) =>
 this.pattern = pattern ?? throw new ArgumentNullException(nameof(pattern));
Beispiel #17
0
 protected RegexNamedGroup(RegexObject <T> regex, string name = null, bool isCaptive = true) : base(regex, name, isCaptive)
 {
 }
 public RegexTernary(RegexMultiBranchBranchPredicate <T> condition, RegexObject <T> truePattern, RegexObject <T> falsePattern) :
     base(
 public RegexBalanceGroupOpenItem(RegexObject <T> regex, Func <TSeed> method) : base(regex) =>
     this.method = method ?? throw new ArgumentNullException(nameof(method));
 public RegexUncaptiveGroup(RegexObject <T> regex) : base(regex, null, false)
 {
 }