Exemple #1
0
        public List <string> Apply(TemplateData data)
        {
            bool          condition_is_true = CheckIfLine(rule_param_line);
            List <string> extend_lines      = new List <string>();

            bool find_else = false; //寻找else

            foreach (string s in rule_lines)
            {
                if (s.Trim() == ElseMatchText)
                {
                    find_else = true;
                    continue;
                }
                //寻找其他if
                if (Regex.IsMatch(s, @"@{ELSEIF\(.+\)}"))
                {
                    if (condition_is_true)
                    {
                        find_else = true;
                    }
                    else
                    {
                        condition_is_true = CheckIfLine(s);
                    }
                    continue;
                }

                if (condition_is_true)
                {
                    if (find_else)
                    {
                        break;
                    }
                    extend_lines.Add(s);
                }
                else
                {
                    if (find_else)
                    {
                        extend_lines.Add(s);
                    }
                }
            }

            //继续展开
            List <string>        result       = new List <string>();
            List <ITemplateRule> extend_rules = TemplateRuleParser.Parse(extend_lines);

            foreach (var rule in extend_rules)
            {
                var ss = rule.Apply(data);
                result.AddRange(ss);
            }

            return(result);
        }
Exemple #2
0
        public List <string> Apply(TemplateData data)
        {
            Regex reg = new Regex(@"@{SWITCH\((.+)\)}");
            Match m   = reg.Match(rule_param_line);

            if (!m.Success)
            {
                Console.WriteLine("{0}格式错误 {1}", match_info.rule_type, rule_param_line);
                return(null);
            }

            string condition = m.Groups[1].Value;

            condition = data.ExtraMetaData(condition) as string;
            Regex regcase   = new Regex(@"@{CASE\((.+)\)}");
            bool  find_case = false; //寻找else

            List <string> extend_lines = new List <string>();

            foreach (string s in rule_lines)
            {
                if (regcase.IsMatch(s))
                {
                    if (find_case)
                    {
                        break;
                    }

                    string   casee = regcase.Match(s).Groups[1].Value;
                    string[] ss    = casee.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                    if (ss.Contains(condition))
                    {
                        find_case = true;
                        continue;
                    }
                }
                if (find_case)
                {
                    extend_lines.Add(s);
                }
            }

            //继续展开
            List <string>        result       = new List <string>();
            List <ITemplateRule> extend_rules = TemplateRuleParser.Parse(extend_lines);

            foreach (var rule in extend_rules)
            {
                var ss = rule.Apply(data);
                result.AddRange(ss);
            }

            return(result);
        }
Exemple #3
0
        public bool GenerateCode(IDLMeta meta, string template, string codefile)
        {
            string[] lines = File.ReadAllLines(template);
            //解析规则
            var rules = TemplateRuleParser.Parse(lines.ToList());
            //展开规则
            List <string> code = new List <string>();
            TemplateData  data = new TemplateData();

            data.SetGlobalVariant("Meta", meta);
            //meta变量注入
            foreach (var kv in meta.meta_variant)
            {
                data.SetGlobalVariant(kv.Key, kv.Value);
            }
            foreach (var rule in rules)
            {
                var code_line = rule.Apply(data);
                code.AddRange(code_line);
            }
            //删除旧文件
            if (File.Exists(codefile))
            {
                File.SetAttributes(codefile, FileAttributes.Normal);
                File.Delete(codefile);
            }
            //写入文件
            using (FileStream fs = new FileStream(codefile, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (var line in code)
                    {
                        sw.WriteLine(line);
                    }
                }
            }
            return(true);
        }
Exemple #4
0
        public List <string> Apply(TemplateData data)
        {
            Regex reg = new Regex(@"@{FOREACH\((\w+)\s+IN\s+(.+)\)}");
            Match m   = reg.Match(rule_param_line);

            if (!m.Success)
            {
                Console.WriteLine("{0}格式错误 {1}", match_info.rule_type, rule_param_line);
                return(null);
            }

            string var_name = m.Groups[1].Value;
            string extra    = m.Groups[2].Value;

            object[] var_array = data.ExtraMetaData(extra) as object[];

            List <string> result = new List <string>();
            int           index  = 0;

            foreach (object v in var_array)
            {
                data.SetLocalVariant(var_name, v);
                data.SetLocalVariant("ForeachIndex", index.ToString());
                index++;
                List <string>        res          = new List <string>();
                List <ITemplateRule> extend_rules = TemplateRuleParser.Parse(rule_lines);
                foreach (var rule in extend_rules)
                {
                    var ss = rule.Apply(data);
                    res.AddRange(ss);
                }
                result.AddRange(res);
            }


            return(result);
        }