public static void Debug(ParsedCode parsed) { Console.WriteLine("ParsedCode Output: -----------------"); Console.WriteLine(parsed.Output.Combine(parsed.Options)); Console.WriteLine("ParsedCode Variables: ---------------"); foreach (var kv in parsed.ParseActions.Where(act => act.Token == ParserToken.Declaration)) { var expr = (VariableDeclarationExpression)kv.Related.First(); Console.WriteLine($"{expr.Name}:\t\t{expr.Right.ToString()}"); } }
/// <summary> /// Writes all code and their relations to projects to CSV files. /// </summary> /// <param name="skip">the number of projects to skip before parsing</param> /// <param name="limit">the number of projects to parse code for</param> public void WriteCode(int skip, int limit) { DirectoryInfo[] projects = FileStore.GetDirectories(Resources.CodeDirectory); int projectTotal = projects.Length; int projectCurrent = 0; Logger.Log("Parsing code of " + projectTotal + " projects to CSV."); using (CsvWriter commandWriter = new CsvWriter(FileStore.GetAbsolutePath(Resources.CommandsCsv), new[] { "scriptId", "projectId", "date", "depth", "scopeType", "scopeName", "command", "param1", "param2", "param3", "param4", "param5", "param6", "param7", "param8", "param9", "param10", "param11", "param12", "param13", "param14", "param15", "param16", "param17", "param18", "param19", "param20" }, int.Parse(Resources.CommandsCsvLinesPerFile))) using (CsvWriter scriptWriter = new CsvWriter(FileStore.GetAbsolutePath(Resources.ScriptsCsv), new[] { "scriptId", "projectId", "date", "scopeType", "scopeName", "lineCount" }, int.Parse(Resources.ScriptsCsvLinesPerFile))) using (CsvWriter procedureWriter = new CsvWriter(FileStore.GetAbsolutePath(Resources.ProceduresCsv), new[] { "projectId", "date", "scopeType", "scopeName", "name", "argumentCount" }, int.Parse(Resources.ProceduresCsvLinesPerFile))) { foreach (DirectoryInfo project in projects) { projectCurrent++; if (projectCurrent <= skip) { continue; } if (limit > 0 && projectCurrent > skip + limit) { break; } int projectId = int.Parse(project.Name); Logger.Log(LoggerHelper.FormatProgress("Parsing code of project " + projectId, projectCurrent, projectTotal)); foreach (FileInfo codeFile in project.GetFiles()) { string code = File.ReadAllText(codeFile.FullName); string codeDate = codeFile.Name.Substring(0, codeFile.Name.Length - 5); ParsedCode parsedCode = ParseCode(projectId, DateTime.Parse(codeDate), code); WriteAllToCsv(commandWriter, parsedCode.Commands); WriteAllToCsv(scriptWriter, parsedCode.Scripts); WriteAllToCsv(procedureWriter, parsedCode.Procedures); } } } }
/// <summary> /// Compiles the lists of commands, scripts, and procedures in the given code. /// </summary> /// <param name="projectId">the id of the code's project</param> /// <param name="date">the date the code was updated</param> /// <param name="rawCode">the complete code of a project</param> /// <returns>the list of commands, scripts, and procedures in the given code</returns> private static ParsedCode ParseCode(int projectId, DateTime date, string rawCode) { ParsedCode parsedCode = new ParsedCode(); // Procedure definitions in root JObject code = JObject.Parse(rawCode); { JArray scripts = code.GetValue("scripts") as JArray; if (scripts != null) { foreach (JArray scriptArray in scripts.OfType <JArray>()) { Script script = new Script(projectId, date, scriptArray[2] as JArray, ScopeType.Stage, "stage"); parsedCode.Join(ParseScript(script)); } } } // Procedure definitions in sprites JArray sprites = (JArray)code.GetValue("children"); foreach (JObject sprite in sprites.OfType <JObject>()) { if (sprite["objName"] == null) { continue; } string spriteName = sprite["objName"].ToString(); JArray scripts = sprite.GetValue("scripts") as JArray; if (scripts != null) { foreach (JArray scriptArray in scripts.OfType <JArray>()) { Script script = new Script(projectId, date, scriptArray[2] as JArray, ScopeType.Sprite, spriteName); parsedCode.Join(ParseScript(script)); } } } return(parsedCode); }
/// <summary> /// Compiles the given script. /// </summary> /// <param name="script">a script</param> /// <returns>the list of commands and procedures in the given script, and the script itself</returns> private static ParsedCode ParseScript(Script script) { ParsedCode scriptCode = new ParsedCode(); ScopeType scopeType = script.ScopeType; string scopeName = script.ScopeName; int indent = 0; scriptCode.Join(ParseScripts(script, script.Code, ref scopeType, ref scopeName, ref indent)); scriptCode.Scripts.Add(new List <object> { script.ScriptId, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), scopeType, scopeName, scriptCode.Commands.Count }); return(scriptCode); }
public void Join(ParsedCode parsedCode) { Commands.AddRange(parsedCode.Commands); Scripts.AddRange(parsedCode.Scripts); Procedures.AddRange(parsedCode.Procedures); }
/// <summary> /// Recursively compiles the lists of commands and procedures in the given script. /// </summary> /// <param name="script">a script</param> /// <param name="scripts">the script's code</param> /// <param name="scopeType">the type of the current scope</param> /// <param name="scopeName">the name of the current scope</param> /// <param name="depth">the current recursive depth</param> /// <returns>the lists of commands and procedures in the given script</returns> private static ParsedCode ParseScripts(Script script, JArray scripts, ref ScopeType scopeType, ref string scopeName, ref int depth) { ParsedCode parsedCode = new ParsedCode(); List <object> command = new List <object> { 0, script.ScriptId, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), depth, scopeType, scopeName }; bool added = false; int i = 0; foreach (JToken innerScript in scripts) { if (innerScript is JValue) { i++; added = true; command.Add(innerScript); } else if (innerScript is JArray) { JArray array = (JArray)innerScript; if (AllOneField(array)) { if (!array.Any()) { command.Add("[]"); } else { int newDepth = depth + 1; parsedCode.Join(ParseScripts(script, array, ref scopeType, ref scopeName, ref newDepth)); } } else { if (array.Any() && array[0].ToString() == "procDef") { i++; added = true; command.Add("procdef"); parsedCode.Procedures.Add(new List <object> { 0, script.ProjectId, script.Date.ToString("yyyy-MM-dd"), scopeType, scopeName, array[1].ToString(), array[2].Count() }); scopeType = ScopeType.ProcDef; scopeName = array[1].ToString(); } else { int newDepth = depth + 1; parsedCode.Join( ParseScripts(script, array, ref scopeType, ref scopeName, ref newDepth)); } } } } for (; i < ParamCount + 1; i++) { command.Add(""); } if (added) { parsedCode.Commands.Add(command); } return(parsedCode); }