Exemple #1
0
        /// <summary>
        /// 规则转换
        /// </summary>
        /// <param name="text">规则内容</param>
        /// <returns></returns>
        public static List <RuleDefination> ParseRules(string text)
        {
            List <RuleDefination> rules = new List <RuleDefination>();

            if (text == null)
            {
                return(rules);
            }
            if (text.Trim().Length <= 0)
            {
                return(rules);
            }

            string[] lines = text.Split("\r\n".ToCharArray());
            if (lines == null)
            {
                return(rules);
            }
            if (lines.Length == 0)
            {
                return(rules);
            }

            List <string> filteredLines = ParseHelper.FilterEmptyRow(lines);

            RuleDefination rule      = null;
            int            ruleIndex = -1;

            for (int i = 0; i < filteredLines.Count; i++)
            {
                string line = filteredLines[i];

                if (line.Trim().Length > 4 && line.Substring(0, 4) == "rule")
                {
                    ruleIndex = i;

                    rule          = new RuleDefination();
                    rule.RuleName = line.Substring(5).Trim('"');
                }
                else if (line.Trim().Length >= 8 && line.Substring(0, 8) == "end rule")
                {
                    if (rule == null)
                    {
                        throw new Exception("Rule Parse error");
                    }

                    if (rule != null)
                    {
                        rule.RuleContent = ParseHelper.GetInnerRawContent(filteredLines, ruleIndex + 1, i - 1);
                        rules.Add(rule);

                        ruleIndex = -1;
                    }
                }
            }

            if (rules.Count == 0)
            {
                throw new Exception("Rule cannot be empty");
            }

            return(rules);
        }
Exemple #2
0
        /// <summary>
        /// 转换规则
        /// </summary>
        /// <param name="text">所有规则函数</param>
        /// <returns></returns>
        public static List <RuleRegion> ParseRegions(string text)
        {
            List <RuleRegion> regions = new List <RuleRegion>();

            if (text == null)
            {
                return(regions);
            }
            if (text.Trim().Length <= 0)
            {
                return(regions);
            }
            //换行,回车作为函数行分隔
            string[] lines = text.Split("\r\n".ToCharArray());
            if (lines == null)
            {
                return(regions);
            }
            if (lines.Length == 0)
            {
                return(regions);
            }
            //去除空白行
            List <string> filteredLines = ParseHelper.FilterEmptyRow(lines);

            RuleRegion region      = null;
            int        regionIndex = -1;

            for (int i = 0; i < filteredLines.Count; i++)
            {
                string line = filteredLines[i];

                if (line.Trim().Length > 7 && line.Substring(0, 7) == "#region")
                {
                    regionIndex = i;

                    region            = new RuleRegion();
                    region.RegionName = line.Substring(8).Trim('"');
                }
                else if (line.Trim().Length >= 10 && line.Substring(0, 10) == "#endregion")
                {
                    if (region == null)
                    {
                        throw new Exception("Region Parse error");
                    }

                    if (region != null)
                    {
                        region.RegionContent = ParseHelper.GetInnerRawContent(filteredLines, regionIndex + 1, i - 1);
                        regions.Add(region);

                        regionIndex = -1;
                    }
                }
            }

            if (regions.Count == 0)
            {
                throw new Exception("Region cannot be empty");
            }

            return(regions);
        }