예제 #1
0
        /// <summary>
        /// Compiles the Provided source array into a single file. And removes all remaining statements
        /// </summary>
        /// <param name="src">The Array of Sourcescripts that need to be compiled.</param>
        /// <returns>A compiled list out of the passed sourcescripts</returns>
        private string[] Compile(ISourceScript[] src, bool restartTimer)
        {
            if (restartTimer)
            {
                Timer.GlobalTimer.Restart();
            }

            long old = Timer.MS;

            Logger.Log(PPLogType.Log, Verbosity.Level2, "Starting Compilation of File Tree...");
            List <string> ret = new List <string>();

            for (int i = src.Length - 1; i >= 0; i--)
            {
                string[] sr = src[i].GetSource();
                if (sr != null && sr.Length != 0)
                {
                    ret.AddRange(sr);
                }
            }

            Logger.Log(PPLogType.Log, Verbosity.Level2, "Finished Compilation...");
            //this.Log(PPLogType.Log, Verbosity.LEVEL3, "Cleaning up: {0}", CleanUpList.Unpack(", "));

            string[] rrr = Utils.RemoveStatements(ret, CleanUpList.ToArray()).ToArray();

            Logger.Log(PPLogType.Progress, Verbosity.Level1, "Finished Compiling {1} Files({0}ms)", Timer.MS - old,
                       src.Length);
            Logger.Log(PPLogType.Log, Verbosity.Level2, "Total Lines: {0}", rrr.Length);
            return(rrr);
        }
예제 #2
0
        public override bool FullScriptStage(ISourceScript file, ISourceManager sourceManager, IDefinitions defs)
        {
            if (!file.HasValueOfType <string[]>("genParams"))
            {
                return(true); //No error, we just dont have any generic parameters to replace.
            }

            string[] genParams = file.GetValueFromCache <string[]>("genParams");

            Logger.Log(PPLogType.Log, Verbosity.Level5, "Discovering Generic Keywords...");
            if (genParams != null && genParams.Length > 0)
            {
                for (int i = genParams.Length - 1; i >= 0; i--)
                {
                    Logger.Log(PPLogType.Log, Verbosity.Level6, "Replacing Keyword {0}{1} with {2} in file {3}",
                               GenericKeyword, i, genParams[i], file.GetKey());
                    Utils.ReplaceKeyWord(file.GetSource(), genParams[i],
                                         GenericKeyword + i);
                }
            }


            Logger.Log(PPLogType.Log, Verbosity.Level5, "Generic Keyword Replacement Finished");

            return(true);
        }
예제 #3
0
        public override string LineStage(string source)
        {
            if (Utils.IsStatement(source, WarningKeyword))
            {
                string err = Utils.SplitAndRemoveFirst(source, Separator).Unpack(" ");

                Logger.Log(LogType.Error, $"Warning: {err}", 1);
                if (ThrowOnWarning)
                {
                    throw new ErrorException(err);
                }

                return("");
            }


            if (Utils.IsStatement(source, ErrorKeyword))
            {
                string err = Utils.SplitAndRemoveFirst(source, Separator).Unpack(" ");
                Logger.Log(LogType.Error, $"Error {err}", 1);
                if (ThrowOnError)
                {
                    throw new ErrorException(err);
                }

                return("");
            }

            return(source);
        }
예제 #4
0
        public override bool FullScriptStage(ISourceScript file, ISourceManager sourceManager, IDefinitions defs)
        {
            if (!file.HasValueOfType <string[]>("genParams"))
            {
                return(true); //No error, we just dont have any generic parameters to replace.
            }

            string[] genParams = file.GetValueFromCache <string[]>("genParams");

            Logger.Log(LogType.Log, "Discovering Generic Keywords...", PLUGIN_MIN_SEVERITY);
            if (genParams != null && genParams.Length > 0)
            {
                for (int i = genParams.Length - 1; i >= 0; i--)
                {
                    Logger.Log(LogType.Log,
                               $"Replacing Keyword {GenericKeyword}{i} with {genParams[i]} in file {file.GetKey()}",
                               PLUGIN_MIN_SEVERITY + 1);
                    Utils.ReplaceKeyWord(file.GetSource(), genParams[i],
                                         GenericKeyword + i);
                }
            }


            Logger.Log(LogType.Log, "Generic Keyword Replacement Finished", PLUGIN_MIN_SEVERITY);

            return(true);
        }
예제 #5
0
        private bool EvaluateConditional(string expression, IDefinitions defs)
        {
            string condition = FixCondition(Utils.SplitAndRemoveFirst(expression, Separator).Unpack(Separator));

            string[] cs = condition.Pack(Separator).ToArray();
            return(EvaluateConditional(cs, defs));
        }
예제 #6
0
        private ImportResult ComputeNameAndKey_Generic(string[] vars, string currentPath)
        {
            ImportResult ret = new ImportResult();

            string filePath = "";

            if (!Utils.TryResolvePathIncludeParameter(vars))
            {
                return(ret);
            }

            string[] genParams = vars.Length > 1 ? vars.SubArray(1, vars.Length - 1).ToArray() : new string[0];

            string rel = Path.Combine(currentPath, vars[0]);
            string key = Path.GetFullPath(rel);

            filePath = key;
            key     += genParams.Length > 0 ? "." + genParams.Unpack(Separator) : "";
            if (genParams.Length != 0)
            {
                ret.SetValue("genParams", genParams);
            }
            ret.SetValue("filename", filePath);
            ret.SetValue("key", key);
            ret.SetResult(true);
            return(ret);
        }
예제 #7
0
        private bool GetISourceScript(ISourceManager manager, string statement, string currentPath,
                                      out List <ISourceScript> scripts)
        {
            string[] vars = Utils.SplitAndRemoveFirst(statement, Separator);

            scripts = new List <ISourceScript>();
            if (vars.Length != 0)
            {
                ImportResult importInfo = manager.GetComputingScheme()(vars, currentPath);
                if (!importInfo)
                {
                    Logger.Log(LogType.Error, "Invalid Include Statement", 1);
                    return(false);
                }

                string filepath = importInfo.GetString("filename");
                importInfo.RemoveEntry("filename");
                string key = importInfo.GetString("key");
                importInfo.RemoveEntry("key");
                string originalDefinedName = importInfo.GetString("definedname");
                importInfo.RemoveEntry("definedname");


                IFileContent cont = new FilePathContent(filepath, originalDefinedName);
                cont.SetKey(key);
                if (manager.TryCreateScript(out ISourceScript iss, Separator, cont, importInfo))
                {
                    scripts.Add(iss);
                }


                for (int index = scripts.Count - 1; index >= 0; index--)
                {
                    ISourceScript sourceScript = scripts[index];
                    if (sourceScript.GetFileInterface().HasValidFilepath&&
                        !Utils.FileExistsRelativeTo(currentPath, sourceScript.GetFileInterface()))
                    {
                        Logger.Log(LogType.Error, $"Could not find File: {sourceScript.GetFileInterface()}", 1);
                        scripts.RemoveAt(index);
                    }
                }


                return(true);
            }


            return(false);
        }
예제 #8
0
        private string FixCondition(string line)
        {
            Logger.Log(LogType.Log, $"Fixing expression: {line}", PLUGIN_MIN_SEVERITY + 7);


            string r = line;

            r = SurroundWithSpaces(r, OrOperator);
            r = SurroundWithSpaces(r, AndOperator);
            r = SurroundWithSpaces(r, "(");
            r = SurroundWithSpaces(r, ")");
            string rr = Utils.RemoveExcessSpaces(r, Separator);

            Logger.Log(LogType.Log, $"Fixed condition(new): {rr}", PLUGIN_MIN_SEVERITY + 7);
            return(rr);
        }
예제 #9
0
        private string FixCondition(string line)
        {
            Logger.Log(PPLogType.Log, Verbosity.Level6, "Fixing expression: {0}", line);


            string r = line;

            r = SurroundWithSpaces(r, OrOperator);
            r = SurroundWithSpaces(r, AndOperator);
            r = SurroundWithSpaces(r, "(");
            r = SurroundWithSpaces(r, ")");
            string rr = Utils.RemoveExcessSpaces(r, Separator);

            Logger.Log(PPLogType.Log, Verbosity.Level6, "Fixed condition(new): {0}", rr);
            return(rr);
        }
예제 #10
0
파일: CLI.cs 프로젝트: codacy-badger/Byt3
        /// <summary>
        /// Implements the log params syntax
        /// int:bool
        /// default: -1:true
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static KeyValuePair <int, bool> ParseLogParams(string input)
        {
            int  mask      = -1;
            bool timestamp = true;

            string[] vars = input.Split(new[] { ':' });
            if (vars.Length > 0)
            {
                if (vars[0] != "all")
                {
                    mask = (int)Utils.Parse(typeof(PPLogType), vars[0], -1);
                }
                if (vars.Length > 1)
                {
                    bool.TryParse(vars[1], out timestamp);
                }
            }
            return(new KeyValuePair <int, bool>(mask, timestamp));
        }
예제 #11
0
        public override string LineStage(string source)
        {
            if (Utils.IsStatement(source, WarningKeyword))
            {
                string err = Utils.SplitAndRemoveFirst(source, Separator).Unpack(" ");

                Logger.Log(PPLogType.Error, Verbosity.Level1, "Warning: {0}", err);
                return("");
            }


            if (Utils.IsStatement(source, ErrorKeyword))
            {
                string err = Utils.SplitAndRemoveFirst(source, Separator).Unpack(" ");
                Logger.Log(PPLogType.Error, Verbosity.Level1, "Error {0}", err);
                return("");
            }

            return(source);
        }
예제 #12
0
        /// <summary>
        /// The default implementation of the key matching calculation
        /// </summary>
        /// <param name="vars">The import string in a source script</param>
        /// <param name="currentPath">the current path of the preprocessor</param>
        /// <returns>A result object.</returns>
        private ImportResult ComputeFileNameAndKey_Default(string[] vars, string currentPath)
        {
            ImportResult ret = new ImportResult();

            if (!Utils.TryResolvePathIncludeParameter(vars))
            {
                return(ret);
            }

            string rel = Path.Combine(currentPath, vars[0]);
            string key = Path.GetFullPath(rel);

            ret.SetValue("definedname", vars[0]);
            ret.SetValue("filename", key);


            ret.SetValue("key", key);
            ret.SetResult(true);

            return(ret);
        }
예제 #13
0
        private bool TryParseDecodeStatement(string line, out TextEncoding encoding, out string[] parameter)
        {
            string[] data = line.Replace(BlockDecodeStartKeyword, "").Replace(BlockEncodeStartKeyword, "").Trim()
                            .Split(' ');

            parameter = new string[0];
            if (data.Length == 0 || !Encoders.TryFindByKey(data[0], out encoding))
            {
                Logger.Log(PPLogType.Error, Verbosity.Level1, "Decode block has no Specified decoding scheme.");

                encoding  = null;
                parameter = null;
                return(false);
            }

            if (data.Length > 1)
            {
                parameter = Utils.SplitAndRemoveFirst(data.Unpack(" "), " ");
            }


            return(true);
        }
예제 #14
0
        /// <summary>
        /// Compiles the Provided source array into a single file. And removes all remaining statements
        /// </summary>
        /// <param name="src">The Array of Sourcescripts that need to be compiled.</param>
        /// <returns>A compiled list out of the passed sourcescripts</returns>
        private string[] Compile(ISourceScript[] src, bool restartTimer)
        {
            if (restartTimer)
            {
                Timer.GlobalTimer.Restart();
            }

            Logger.Log(LogType.Log, "Starting Compilation of File Tree...", 4);
            List <string> ret = new List <string>();

            for (int i = src.Length - 1; i >= 0; i--)
            {
                string[] sr = src[i].GetSource();
                if (sr != null && sr.Length != 0)
                {
                    ret.AddRange(sr);
                }
            }


            string[] rrr = Utils.RemoveStatements(ret, CleanUpList.ToArray()).ToArray();

            return(rrr);
        }
예제 #15
0
        private bool GetISourceScript(ISourceManager manager, string statement, string currentPath,
                                      out List <ISourceScript> scripts)
        {
            string[] vars = Utils.SplitAndRemoveFirst(statement, Separator);

            scripts = new List <ISourceScript>();
            if (vars.Length != 0)
            {
                ImportResult importInfo = manager.GetComputingScheme()(vars, currentPath);
                if (!importInfo)
                {
                    Logger.Log(PPLogType.Error, Verbosity.Level1, "Invalid Include Statement");
                    return(false);
                }

                string filepath = importInfo.GetString("filename");
                importInfo.RemoveEntry("filename");
                string key = importInfo.GetString("key");
                importInfo.RemoveEntry("key");


                if (filepath.EndsWith("\\*") || filepath.EndsWith("/*"))
                {
                    string[] files = IOManager.GetFiles(filepath.Substring(0, filepath.Length - 2));
                    foreach (string file in files)
                    {
                        IFileContent cont = new FilePathContent(file);
                        cont.SetKey(key);
                        if (manager.TryCreateScript(out ISourceScript iss, Separator, cont, importInfo))
                        {
                            scripts.Add(iss);
                        }
                    }
                }
                else
                {
                    IFileContent cont = new FilePathContent(filepath);
                    cont.SetKey(key);
                    if (manager.TryCreateScript(out ISourceScript iss, Separator, cont, importInfo))
                    {
                        scripts.Add(iss);
                    }
                }


                for (int index = scripts.Count - 1; index >= 0; index--)
                {
                    ISourceScript sourceScript = scripts[index];
                    if (sourceScript.GetFileInterface().HasValidFilepath&&
                        !Utils.FileExistsRelativeTo(currentPath, sourceScript.GetFileInterface()))
                    {
                        Logger.Log(PPLogType.Error, Verbosity.Level1, "Could not find File: {0}",
                                   sourceScript.GetFileInterface());
                        scripts.RemoveAt(index);
                    }
                }


                return(true);
            }


            return(false);
        }
예제 #16
0
        public override bool FullScriptStage(ISourceScript script, ISourceManager sourceManager, IDefinitions defs)
        {
            Logger.Log(LogType.Log, "Disovering Include Statments...", PLUGIN_MIN_SEVERITY);
            List <string> source      = script.GetSource().ToList();
            string        currentPath = Path.GetDirectoryName(script.GetFileInterface().GetFilePath());
            bool          hasIncludedInline;

            do
            {
                hasIncludedInline = false;
                for (int i = source.Count - 1; i >= 0; i--)
                {
                    if (Utils.IsStatement(source[i], IncludeInlineKeyword))
                    {
                        Logger.Log(LogType.Log, "Found Inline Include Statement...", PLUGIN_MIN_SEVERITY + 1);
                        string[] args = Utils.SplitAndRemoveFirst(source[i], Separator);
                        if (args.Length == 0)
                        {
                            Logger.Log(LogType.Error, "No File Specified", 1);
                            continue;
                        }

                        if (Utils.FileExistsRelativeTo(currentPath, args[0]))
                        {
                            Logger.Log(LogType.Log, "Replacing Inline Keyword with file content",
                                       PLUGIN_MIN_SEVERITY + 2);
                            source.RemoveAt(i);

                            source.InsertRange(i, IOManager.ReadAllLines(Path.Combine(currentPath, args[0])));
                            hasIncludedInline = true;
                        }
                        else
                        {
                            Logger.Log(LogType.Error, $"File does not exist: {args[0]}", 1);
                        }
                    }
                }

                script.SetSource(source.ToArray());
            } while (hasIncludedInline);


            string[] incs = Utils.FindStatements(source.ToArray(), IncludeKeyword);

            foreach (string includes in incs)
            {
                Logger.Log(LogType.Log, $"Processing Statement: {includes}", PLUGIN_MIN_SEVERITY + 1);
                bool tmp = GetISourceScript(sourceManager, includes, currentPath, out List <ISourceScript> sources);
                if (tmp)
                {
                    foreach (ISourceScript sourceScript in sources)
                    {
                        Logger.Log(LogType.Log,
                                   $"Processing Include: {Path.GetFileName(sourceScript.GetFileInterface().GetKey())}",
                                   PLUGIN_MIN_SEVERITY + 2);

                        if (!sourceManager.IsIncluded(sourceScript))
                        {
                            sourceManager.AddToTodo(sourceScript);
                        }
                        else
                        {
                            sourceManager.FixOrder(sourceScript);
                        }
                    }
                }
                else
                {
                    return
                        (false); //We crash if we didnt find the file. but if the user forgets to specify the path we will just log the error
                }
            }

            Logger.Log(LogType.Log, "Inclusion of Files Finished", PLUGIN_MIN_SEVERITY);
            return(true);
        }
예제 #17
0
        public override bool FullScriptStage(ISourceScript file, ISourceManager todo, IDefinitions defs)
        {
            Logger.Log(LogType.Log,
                       $"Starting Condition Solver passes on file: {Path.GetFileName(file.GetFileInterface().GetKey())}",
                       PLUGIN_MIN_SEVERITY);
            bool          ret             = true;
            int           openIf          = 0;
            bool          foundConditions = false;
            bool          elseIsValid     = false;
            bool          expectEndOrIf   = false;
            List <string> lastPass        = file.GetSource().ToList();
            List <string> solvedFile      = new List <string>();
            int           passCount       = 0;

            do
            {
                passCount++;
                Logger.Log(LogType.Log, $"Starting Condition Solver pass: {passCount}", PLUGIN_MIN_SEVERITY + 1);

                foundConditions = false;
                elseIsValid     = false;
                for (int i = 0; i < lastPass.Count; i++)
                {
                    string line = lastPass[i].TrimStart();
                    if (IsKeyWord(line, StartCondition))
                    {
                        Logger.Log(LogType.Log, $"Found a {StartCondition} Statement", PLUGIN_MIN_SEVERITY + 2);
                        KeyValuePair <bool, int> prep = PrepareForConditionalEvaluation(line, defs,
                                                                                        lastPass, i,
                                                                                        solvedFile);

                        elseIsValid = prep.Key;
                        i          += prep.Value;

                        openIf++;
                        foundConditions = true;
                        expectEndOrIf   = false;
                    }
                    else if (elseIsValid && IsKeyWord(line, ElseIfCondition))
                    {
                        Logger.Log(LogType.Log, $"Found a {ElseIfCondition} Statement", PLUGIN_MIN_SEVERITY + 2);
                        if (!expectEndOrIf && openIf > 0)
                        {
                            KeyValuePair <bool, int> prep = PrepareForConditionalEvaluation(line, defs,
                                                                                            lastPass, i,
                                                                                            solvedFile);

                            elseIsValid     = prep.Key;
                            i              += prep.Value;
                            foundConditions = true;
                        }
                        else if (expectEndOrIf)
                        {
                            Logger.Log(LogType.Error, $"A {ElseCondition} can not be followed by an {ElseIfCondition}",
                                       1);
                            ret = false;
                            break;
                        }
                        else
                        {
                            Logger.Log(LogType.Error, $"A {ElseIfCondition} should be preceeded by a {StartCondition}",
                                       1);
                            ret = false;
                            break;
                        }
                    }
                    else if (IsKeyWord(line, ElseCondition))
                    {
                        if (openIf > 0)
                        {
                            Logger.Log(LogType.Log, $"Found a {ElseCondition} Statement", PLUGIN_MIN_SEVERITY + 2);
                            int size = GetBlockSize(lastPass, i);
                            if (elseIsValid)
                            {
                                solvedFile.AddRange(lastPass.SubArray(i + 1, size));
                                Logger.Log(LogType.Log, "Adding Branch To Solved File.", PLUGIN_MIN_SEVERITY + 3);
                            }
                            else
                            {
                                Logger.Log(LogType.Log,
                                           "Ignored since a previous condition was true", PLUGIN_MIN_SEVERITY + 3);
                            }

                            i += size;
                            foundConditions = true;
                            expectEndOrIf   = true;
                        }
                        else
                        {
                            Logger.Log(LogType.Error, $"A {ElseCondition} should be preceeded by a {StartCondition}",
                                       1);
                            ret = false;
                            break;
                        }
                    }
                    else if (IsKeyWord(line, EndCondition))
                    {
                        if (openIf > 0)
                        {
                            expectEndOrIf = false;
                            openIf--;
                        }
                        else
                        {
                            ret = false;

                            Logger.Log(LogType.Error, $"A {EndCondition} should be preceeded by a {StartCondition}", 1);
                            break;
                        }
                    }
                    else if (EnableDefine &&
                             line.StartsWith(DefineKeyword))
                    {
                        Logger.Log(LogType.Log, $"Found a {DefineKeyword} Statement", PLUGIN_MIN_SEVERITY + 2);
                        defs.Set(Utils.SplitAndRemoveFirst(line, Separator));
                        solvedFile.Add(lastPass[i]);
                    }
                    else if (EnableUndefine &&
                             line.StartsWith(UndefineKeyword))
                    {
                        Logger.Log(LogType.Log, $"Found a {UndefineKeyword} Statement", PLUGIN_MIN_SEVERITY + 2);
                        defs.Unset(Utils.SplitAndRemoveFirst(line, Separator));
                        solvedFile.Add(lastPass[i]);
                    }
                    else
                    {
                        solvedFile.Add(lastPass[i]);
                    }
                }

                if (ret)
                {
                    lastPass = solvedFile;
                }
                else
                {
                    break;
                }

                solvedFile = new List <string>();
            } while (foundConditions);

            file.SetSource(lastPass.ToArray());


            Logger.Log(LogType.Log, "Conditional Solver Finished", PLUGIN_MIN_SEVERITY);

            return(ret);
        }