예제 #1
0
파일: RuleParser.cs 프로젝트: staoran/Ruler
        public static List <Rule> ParseRules(string text)
        {
            List <Rule> rules = new List <Rule>();

            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);

            Rule 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 Rule();
                    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);
        }
예제 #2
0
        public static List <Region> ParseRegions(string text)
        {
            List <Region> regions = new List <Region>();

            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);

            Region 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 Region();
                    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);
        }