Пример #1
0
        public string Compile(ISomContext somContext)
        {
            string content = somContext.Content;

            for (int i = 12; i > 0; i -= 4)
            {
                var parsed = new SomDocParser(i).Parse(somContext);
                foreach (var pr in parsed)
                {
                    Type typ = Type.GetType($"{pr.CommandType.FullName}, SOM");
                    CompilableCtorMeta ccm  = GetCompilableCtorMeta(pr.CommandType);
                    ConstructorInfo    ctor = GetConstructorInfo(typ);

                    var parseItem = pr.Parsed;
                    var oparms    = pr.Parms(ctor.GetParameters());

                    somContext.Logger.Information("SomTagInterpreter: Indent{i} {o} {p}", i, pr.CommandType.FullName, string.Join(", ", oparms.ToArray()));

                    ICompilable obj = (ICompilable)Activator.CreateInstance(typ, oparms.ToArray());
                    somContext.Content = parseItem;
                    parseItem          = obj.Compile(somContext);
                    parseItem          = RemoveTags(parseItem);
                    content            = content.Replace(pr.Parsed, parseItem);
                    somContext.Content = content;
                    if (pr.Options.Verbose)
                    {
                        somContext.Cache.Write(content);
                    }
                }
            }
            return(somContext.Content);
        }
Пример #2
0
        public string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();

            content = content.Replace($"\r", $"\n");
            content = content.Replace($"\n\n", $"\n");
            foreach (var line in base.ParseLines(content))
            {
                if (Regex.IsMatch(line, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendLine(line);
                    continue;
                }
                string target  = line;
                string pattern = "([^\\d]|^)(" + _incrementPattern + ")([^\\d]|$)";
                if (Regex.IsMatch(target, pattern))
                {
                    target = Regex.Replace(target, pattern,
                                           m =>
                    {
                        int nextint = (_base - _seed) + Convert.ToInt32(m.Groups[2].Value) + 0;
                        return($"{m.Groups[1].Value}{nextint}{m.Groups[3].Value}");
                    }
                                           , RegexOptions.Singleline);
                }
                ;
                result.AppendLine($"{target}");
            }
            return(result.ToString());
        }
Пример #3
0
        public string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();
            var           lines   = Regex.Split(content, $"\r|\n");

            foreach (var line in ParseLines(content))
            {
                if (Regex.IsMatch(line, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendLine(line);
                    continue;
                }
                string target  = line;
                string pattern = "([^\\d]|^)(" + _pattern + ")([^\\d]|$)";
                if (Regex.IsMatch(target, pattern))
                {
                    target = Regex.Replace(target, pattern,
                                           m =>
                    {
                        int nextint = (_amount) + Convert.ToInt32(m.Groups[2].Value) + 0;
                        return($"{m.Groups[1].Value}{nextint}{m.Groups[3].Value}");
                    }
                                           , RegexOptions.Singleline);
                }
                ;
                target = Regex.Replace(target, $"\r|\n", "");
                if (!string.IsNullOrWhiteSpace(target))
                {
                    result.AppendLine(target);
                }
            }
            return(result.ToString());
        }
Пример #4
0
        public virtual string Compile(ISomContext somContext)
        {
            string        content      = somContext.Content;
            StringBuilder result       = new StringBuilder();
            string        matchpattern = "";
            Match         match        = Regex.Match(content, $@".*som!%(\d) -f (.*).*!som");

            if (!match.Success)
            {
                return(content);
            }
            else
            {
                GroupCollection groups = match.Groups;
                matchpattern = groups[0].Value.TrimTrailingNewline();
                string[] lines  = content.Split(new[] { matchpattern }, StringSplitOptions.None);
                int      modval = Convert.ToInt32(groups[1].Value.Replace("%", ""));
                int      index  = 0;
                foreach (var line in lines)
                {
                    index++;
                    string append = ((index % modval == 0) && (index <= lines.Length)) ? groups[2].Value : "";
                    if (line != "")
                    {
                        result.AppendFormat("{0}{1}", line, append);
                    }
                }
            }
            return(result.ToString().Replace(matchpattern, "").TrimTrailingNewline());
        }
Пример #5
0
        public override string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();
            var           keyvals = base.PopulateKeyVals(somContext);

            foreach (var contentline in base.ParseLines(content))
            {
                string line = contentline;
                if (Regex.IsMatch(line, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendLine(line);
                    continue;
                }
                foreach (var item in keyvals)
                {
                    int    cnt     = 0;
                    string pattern = "([^\\d]|^)(" + item.Key + ")([^\\d]|$)";
                    while (Regex.IsMatch(line, pattern))
                    {
                        line = Regex.Replace(line, pattern,
                                             m => $"{m.Groups[1].Value}{item.Value}{m.Groups[3].Value}"
                                             , RegexOptions.Singleline);
                        if (cnt++ > 5)
                        {
                            break;
                        }
                    }
                    ;
                }
                line = Regex.Replace(line, $"\r|\n", "");
                result.AppendLine(line);
            }
            return(result.ToString());
        }
Пример #6
0
        public IEnumerable <string> Parse(ISomContext somContext)
        {
            string content = somContext.Content;

            content = content.Replace("\r", "\n").Replace("\n\n", "\n");
            string[] lines   = content.Split('\n');
            int      linecnt = 0;

            foreach (string line in lines)
            {
                linecnt++;
                foreach (string pattern in _patterns)
                {
                    Match match = Regex.Match(line, pattern);
                    if (match.Success)
                    {
                        StringBuilder result = new StringBuilder();
                        if (somContext.Options.Mode == SomMode.Cache)
                        {
                            result.Append(line + "\n");
                        }
                        else
                        {
                            result.Append($"{line}[LN {linecnt.ToString()}]\n");
                        }
                        yield return(result.ToString().TrimTrailingNewline());
                    }
                }
            }
        }
Пример #7
0
        public ParseTests()
        {
            var config = new TestServices().Configuration;
            var logger = new Mock <ILogger>().Object;
            var cache  = new CacheService(config, logger);

            somContext = new SomContext(config, logger, cache);
        }
Пример #8
0
 public CompileProcessor(
     ICompiler compiler
     , ISomContext somContext
     )
 {
     this.somContext = somContext;
     this.compiler   = compiler;
     this.config     = this.somContext.Config;
     this.logger     = this.somContext.Logger;
 }
Пример #9
0
        public virtual string Compile(ISomContext somContext)
        {
            string content = somContext.Content;
            var    keyvals = PopulateKeyVals(somContext);

            foreach (var item in keyvals)
            {
                content = content.Replace(item.Key, item.Value);
            }
            return(content);
        }
Пример #10
0
        public string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();

            _SchemaProvider = new SchemaProvider(somContext.Config);
            _SchemaProvider.LoadModel(this._modelName);

            var lines = (from s in Regex.Split(content, $@"\r|\n")
                         where !string.IsNullOrWhiteSpace(s)
                         select s).ToList();

            var prefix  = lines[0];
            var postfix = lines[lines.Count() - 1];

            _format = string.Join(' ', lines).Replace(prefix, "").Replace(postfix, "");
            _format = Regex.Replace(_format, @"\s\/n", "\n");
            _format = Regex.Replace(_format, @"\s\/t", "\t");

            IEnumerable <AppModelItem> _AppModelItems = _SchemaProvider
                                                        .AppModelItems.Select(i => i)
                                                        .Where(i => Regex.IsMatch(i.Name, _predpattern)).AsEnumerable();

            if (Regex.IsMatch(_format, $@"^.*\w:\\"))
            {
                foreach (AppModelItem item in _AppModelItems)
                {
                    string path = item.ToStringFormat(_format);
                    path = path.Replace("{1}", item.DataType);
                    path = path.Replace("~", somContext.BasePath);
                    string rte = "";
                    using (TextReader tr = File.OpenText(path))
                        rte = tr.ReadToEnd();
                    string fmt = item.ToStringFormat(rte);
                    result.Append(fmt);
                }
            }
            else
            {
                foreach (AppModelItem item in _AppModelItems)
                {
                    _format = (string.IsNullOrWhiteSpace(_format)) ? "{0}" : _format;
                    result.Append(item.ToStringFormat(_format));
                }
            }
            content = prefix + "\n" + result.ToString() + "\n" + postfix;
            return(content);
        }
Пример #11
0
        public void Process(ISomContext somContext)
        {
            string configPath = config.GetSection("AppSettings:CompileConfig").Value ?? "~";
            string basePath   = config.GetSection("AppSettings:BasePath").Value;
            string configFile = somContext.Options.Path;

            if (!string.IsNullOrEmpty(configFile.ToString()))
            {
                configPath = configPath.Replace("~", basePath);
                if (!configFile.Contains(":"))
                {
                    configFile = $"{configPath}{configFile}";
                }
                configFile = configFile.Replace(@"\\", @"\");
                configFile = (configFile.Contains(".yaml")) ? configFile : $"{configFile}.yaml";
            }
            logger.Information("{o}", configFile);

            string raw   = File.ReadAllText(configFile);
            var    deser = new DeserializerBuilder().WithNamingConvention(PascalCaseNamingConvention.Instance).Build();
            var    def   = deser.Deserialize <CompileDefinition>(raw);

            def.ContentCompilers.ForEach(c =>
            {
                var typ         = AssmTypes().Where(t => t.Name == c.CompilerType && typeof(ICompilable).IsAssignableFrom(t)).FirstOrDefault();
                ICompilable obj = (ICompilable)Activator.CreateInstance(typ, c.Args.ToArray());
                compiler.ContentCompilers.Add(obj);
            });
            def.FilenameCompilers.ForEach(c =>
            {
                var typ         = AssmTypes().Where(t => t.Name == c.CompilerType && typeof(ICompilable).IsAssignableFrom(t)).FirstOrDefault();
                ICompilable obj = (ICompilable)Activator.CreateInstance(typ, c.Args.ToArray());
                compiler.FilenameCompilers.Add(obj);
            });
            def.Compilations.ForEach(c =>
            {
                compiler.FileFilter = c.FileFilter;
                compiler.Source     = (c.Source ?? "~").Replace("~", somContext.BasePath);
                compiler.Dest       = (c.Dest ?? "~").Replace("~", somContext.BasePath);
                compiler.Compile();
            });
            if (somContext.Options.Mode != SomMode.Commit)
            {
                somContext.Cache.Inspect();
            }
        }
Пример #12
0
        public IEnumerable <CommandParseResult> Parse(ISomContext somContext)
        {
            string content = somContext.Content;

            content = $"\n{content}\n";
            MatchCollection mc = Regex.Matches(content, @"\n.*som!" + _parsetag + "\\s?(.*)", RegexOptions.IgnoreCase);

            foreach (Match prefix in mc)
            {
                string match   = content.Substring(prefix.Index, content.Length - prefix.Index);
                Match  postfix = Regex.Match(match, @"" + _parsetag + "!som.*", RegexOptions.IgnoreCase);
                string result  = match.Substring(0, postfix.Index + postfix.Length);
                var    cpr     = new CommandParseResult();
                cpr.Parsed     = result;
                cpr.RawOptions = prefix.Groups[1].Value;
                cpr.Prefix     = prefix;
                cpr.Postfix    = postfix;
                yield return(cpr);
            }
        }
Пример #13
0
        protected Dictionary <string, string> PopulateKeyVals(ISomContext somContext)
        {
            string src = "";
            Dictionary <string, string> KeyVals = new Dictionary <string, string>();

            if (Source.ToLower().EndsWith(".json"))
            {
                Source = Source.Replace("~", somContext.BasePath);
                using (TextReader tr = File.OpenText(Source))
                    src = tr.ReadToEnd();
                KeyVals = JsonConvert.DeserializeObject <Dictionary <string, string> >(src);
                return(KeyVals);
            }
            if (Source.ToLower().EndsWith(".sql"))
            {
                Source = Source.Replace("~", somContext.BasePath);
                using (TextReader tr = File.OpenText(Source))
                    src = tr.ReadToEnd();

                var con = somContext.Config.GetSection("ConnectionStrings")["default"];
                using (SqlConnection conn = new SqlConnection(con))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(src, conn);
                    using (SqlDataReader read = cmd.ExecuteReader())
                        while (read.Read())
                        {
                            if (!KeyVals.ContainsKey(read[0].ToString()))
                            {
                                KeyVals.Add(read[0].ToString(), read[1].ToString());
                            }
                        }
                }
                return(KeyVals);
            }
            if (this.IsValidJson(Source))
            {
                KeyVals = JsonConvert.DeserializeObject <Dictionary <string, string> >(Source);
            }
            return(KeyVals);
        }
Пример #14
0
        public virtual string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();
            int           index   = _seed;

            foreach (var line in base.ParseLines(content))
            {
                if (Regex.IsMatch(line, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendLine(line);
                    continue;
                }
                var m = Regex.Match(line, _pattern);
                if (m.Success)
                {
                    var val = ReSetter(index).ToString();
                    if (m.Groups.Count == 1)
                    {
                        result.AppendFormat("{0}\n", line.Replace(m.Value, val));
                    }
                    if (m.Groups.Count == 2)
                    {
                        val = m.Groups[0].Value.Replace(m.Groups[1].Value, val);
                        result.AppendFormat("{0}\n", line.Replace(m.Value, val));
                    }
                    if (m.Groups.Count == 4)
                    {
                        result.AppendFormat("{0}\n", line.Replace(m.Groups[0].Value, $"{m.Groups[1].Value}{val}{m.Groups[3].Value}"));
                    }

                    index++;
                }
                else
                {
                    result.AppendFormat("{0}\n", line);
                }
            }
            return(result.ToString().TrimTrailingNewline());
        }
Пример #15
0
        public override string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();
            var           keyvals = base.PopulateKeyVals(somContext);

            foreach (var line in base.ParseLines(content))
            {
                string replacement = line;
                if (Regex.IsMatch(replacement, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendLine(replacement);
                    continue;
                }
                foreach (var item in keyvals)
                {
                    var match = Regex.Match(replacement, item.Key);
                    if (match.Success)
                    {
                        replacement = Regex.Replace(
                            replacement
                            , match.Value
                            , m =>
                        {
                            if (m.Groups.Count > 2)
                            {
                                return($"{ m.Groups[1]}{item.Value}{m.Groups[3]}");
                            }
                            else
                            {
                                return(item.Value);
                            }
                        }
                            , RegexOptions.Singleline);
                    }
                }
                result.AppendLine(replacement);
            }
            return(result.ToString().RemoveEmptyLines());
        }
Пример #16
0
        public string Compile(ISomContext somContext)
        {
            string content = somContext.Content;

            if (Regex.IsMatch(content, this._searchPattern, RegexOptions.Singleline))
            {
                content = Regex.Replace(content, this._searchPattern,
                                        m =>
                {
                    if (m.Groups.Count > 0)
                    {
                        return(this._format
                               .Replace("$1", m.Groups[0].Value)
                               .Replace("$2", this._newContent));
                    }
                    return(this._newContent);
                }
                                        , RegexOptions.Singleline);
            }
            ;
            return(content);
        }
Пример #17
0
        public string Compile(ISomContext somContext)
        {
            string content = somContext.Content;
            IParser <CommandParseResult> _Parser = new SomTagParser("SCHEMA");

            foreach (var parseresult in _Parser.Parse(somContext))
            {
                StringBuilder result = new StringBuilder();
                IEnumerable <AppModelItem> _AppModelItems = _SchemaProvider
                                                            .GetModel(parseresult.Options.Model.ToValidSchemaName() ?? _InitModel)
                                                            .AppModelItems.Select(schemaItemProjector)
                                                            .Where(schemaItemPredicate).AsEnumerable();

                if (parseresult.Options.Template != null)
                {
                    foreach (AppModelItem item in _AppModelItems)
                    {
                        string path = item.ToStringFormat(parseresult.Options.Template);
                        path = path.Replace("{1}", item.DataType);
                        path = path.Replace("~", somContext.BasePath);

                        string rte = "";
                        using (TextReader tr = File.OpenText(path))
                            rte = tr.ReadToEnd();
                        string fmt = item.ToStringFormat(rte);
                        result.Append(fmt);
                    }
                }
                else
                {
                    foreach (AppModelItem item in _AppModelItems)
                    {
                        result.Append(item.ToStringFormat(parseresult.Options.Format ?? "{0}"));
                    }
                }
                content = content.Replace(parseresult.Parsed, result.ToString());
            }
            return(content);
        }
Пример #18
0
        public IEnumerable <string> Parse(ISomContext somContext)
        {
            string content      = somContext.Content;
            string _fromPattern = $"(?={_fromWhere})";

            string[] lines = Regex.Split(content, _fromPattern);
            lines = (from fs in lines where Regex.IsMatch(fs, _extractPattern) select fs).ToArray();

            foreach (var line in lines)
            {
                if (Regex.IsMatch(line, _extractPattern))
                {
                    int toPos = line.IndexOf(_toWhere);
                    toPos = (toPos < 0) ? line.Length : toPos + _toWhere.Length;
                    if (toPos > line.Length)
                    {
                        toPos = line.Length;
                    }
                    yield return(string.Format("{0}", line.Substring(0, toPos)));
                }
            }
        }
Пример #19
0
        public IEnumerable <CommandParseResult> Parse(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder sb      = new StringBuilder();

            content = $"\n{content}\n";

            string indent  = @"^ {" + this.indent + "}";
            string pattern = @".*som!(\w+) ?(.+)\n";
            string regex   = $"{indent}{pattern}";
            var    mc      = Regex.Matches(content, regex, RegexOptions.Multiline);

            foreach (Match prefix in mc)
            {
                if (prefix?.Groups.Count > 0)
                {
                    var    CommandName    = prefix.Groups[1].Value;
                    string matchedContent = content.Substring(prefix.Index, content.Length - prefix.Index);
                    string postRegex      = indent + ".*" + CommandName + "!som.*";
                    Match  postfix        = Regex.Match(matchedContent, postRegex, RegexOptions.Multiline);

                    string RawParsed = matchedContent.Substring(0, matchedContent.Length - 1);
                    if (postfix.Success)
                    {
                        RawParsed = matchedContent.Substring(0, postfix.Index + postfix.Length);
                    }

                    var cpr = new CommandParseResult();
                    cpr.Parsed      = RawParsed;
                    cpr.RawOptions  = prefix.Groups[2].Value;
                    cpr.CommandName = CommandName;
                    cpr.CommandType = (from c in _compilables where c.Name == CommandName select c).FirstOrDefault();
                    cpr.Prefix      = prefix;
                    cpr.Postfix     = postfix;
                    yield return(cpr);
                }
            }
        }
Пример #20
0
        public IEnumerable <string> Parse(ISomContext somContext)
        {
            string content = somContext.Content;

            content = content.Replace("\r", "\n").Replace("\n\n", "\n");
            content = $"{new string('\n', _numberOfLines)}{content}{new string('\n', _numberOfLines)}";
            string[] lines      = content.Split('\n');
            int      findingCnt = 0;

            for (int lineIndex = _numberOfLines; lineIndex < lines.Length - _numberOfLines; lineIndex++)
            {
                Match match = Regex.Match(lines[lineIndex], _extractTarget);
                if (match.Success)
                {
                    StringBuilder result = new StringBuilder();
                    findingCnt++;
                    int cursor = lineIndex;
                    for (int takeIndex = lineIndex - _numberOfLines; takeIndex <= lineIndex + _numberOfLines; takeIndex++)
                    {
                        if (takeIndex < lines.Length && takeIndex > 0)
                        {
                            if (somContext.Options.Mode == SomMode.Cache)
                            {
                                result.Append(lines[takeIndex] + "\n");
                            }
                            else
                            {
                                result.Append($"{lines[takeIndex]} [LN {takeIndex.ToString()}]\n");
                            }
                        }
                        cursor = takeIndex;
                    }
                    lineIndex = cursor + 1;
                    yield return(result.ToString().TrimTrailingNewline());
                }
            }
        }
Пример #21
0
        public void Process(ISomContext somContext)
        {
            string basePath   = config.GetSection("AppSettings:BasePath").Value;
            string configPath = (config.GetSection("AppSettings:ParseConfig").Value ?? "~").Replace("~", basePath);
            string configFile = $"{configPath}{somContext.Options.Path}.yaml".Replace(@"\\", @"\");

            logger.Information("{o}", configFile);

            string raw         = File.ReadAllText(configFile);
            var    deser       = new DeserializerBuilder().WithNamingConvention(PascalCaseNamingConvention.Instance).Build();
            var    dfd         = deser.Deserialize <DirectoryParseDefinition>(raw);
            Type   ptype       = (from t in this.IParserTypes where t.Name == dfd.ParseType select t).FirstOrDefault();
            var    ctor_params = ptype.GetConstructors()[0].GetParameters();

            for (int i = 0; i < ctor_params.Count(); i++)
            {
                var val = Convert.ChangeType(dfd.ParseTypeArgs[i], ctor_params[i].ParameterType);
                dfd.ParseTypeArgs[i] = val;
            }
            parser.Directories.AddRange(dfd.Directories);
            parser.Parser       = (IParser <string>)Activator.CreateInstance(ptype, dfd.ParseTypeArgs.ToArray());
            parser.FileFilter   = dfd.FileFilter;
            parser.ResultFormat = dfd.ResultFormat;
            if (string.IsNullOrWhiteSpace(dfd.Dest))
            {
                parser.ParseDirectory();
                somContext.Cache.Write(parser.ToString());
                somContext.Cache.Inspect();
            }
            else
            {
                parser.ParseDirectory();
                dfd.Dest = dfd.Dest.Replace("~", somContext.BasePath);
                using (StreamWriter w = File.AppendText($"{dfd.Dest}")) { }
                File.WriteAllText($"{dfd.Dest}", parser.ToString(), Encoding.Unicode);
            }
        }
Пример #22
0
        public string Compile(ISomContext somContext)
        {
            string        content = somContext.Content;
            StringBuilder result  = new StringBuilder();

            foreach (var line in base.ParseLines(content))
            {
                if (Regex.IsMatch(line, $@"(som!\w+|\w+!som)"))
                {
                    result.AppendFormat("{0}\n", line);
                    continue;
                }
                var rslt = line;
                if (Regex.IsMatch(rslt, this.Pattern))
                {
                    rslt = Regex.Replace(rslt, this.Pattern,
                                         (Match m) => (
                                             m.Groups[0].Value.Replace(m.Groups[1].Value, (this.seed++).ToString())
                                             ));
                }
                result.AppendFormat("{0}\n", rslt);
            }
            return(result.ToString().TrimTrailingNewline());
        }
Пример #23
0
 public DirectoryParser(ISomContext somContext)
 {
     this.somContext = somContext;
     Results         = new Dictionary <string, string>();
     somContext.Cache.Write("");
 }