public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectPublicDeclar"); int index = semi.Contains(";"); if (index != -1) { index = semi.Contains("public"); if (index == -1) { return(true); } CSemiExp local = new CSemiExp(); local.displayNewLines = false; local.Add("public " + semi[index + 1]).Add(semi[index + 2]); index = semi.Contains("="); if (index != -1) { doActions(local); return(true); } index = semi.Contains("("); if (index == -1) { doActions(local); return(true); } } return(false); }
public override bool test(CSemiExp semi) { var typeList = new List <string> { "class", "interface", "struct" }; var currentClassName = _repo.stack.GetContext(e => e.type == "class")?.name; if (currentClassName == null) { return(false); } var possibleDependencies = _classes .Where(e => typeList.Contains(e.type)) .Where(c => c.name != currentClassName) .Select(c => c.name); foreach (var dependency in possibleDependencies) { var tokenIndex = semi.FindFirst(dependency); if (tokenIndex != -1) { var local = new CSemiExp(); local.Add(currentClassName); local.Add(dependency); doActions(local); } } return(false); }
public override bool test(CSemiExp semi) { var currentClassName = _repo.stack.GetContext(e => e.type == "class")?.name; var currentFunctionName = _repo.stack.GetContext(e => e.type == "function")?.name; if (currentClassName == null || currentFunctionName == null) { return(false); } if (!_classMembers.ContainsKey(currentClassName)) { return(false); } var classMembers = _classMembers[currentClassName]; foreach (var member in classMembers) { var tokenIndex = semi.FindFirst(member); if (tokenIndex != -1) { var local = new CSemiExp(); local.Add(currentClassName); local.Add(currentFunctionName); local.Add(member); doActions(local); return(false); } } return(false); }
public override void doAction(CSemiExp semi) { string classScope = repo_.currentClassScope; string functionScope = repo_.currentFunctionScope; if (classScope != null) { for (int i = 0; i < repo_.locations.Count; i++) { Elem tempElem = repo_.locations[i]; //update class variable if (tempElem.name.Equals(classScope) || tempElem.name.Equals(functionScope)) { repo_.locations[i].coupling += 1; //add one more if there is instantiation if (semi.Contains("new") != -1) { repo_.locations[i].coupling += 1; } //break; } } } }
public override void doAction(CSemiExp semi) { (var keyClass, var keyFunction, var member) = (semi[0], semi[1], semi[2]); if (!_repo.FunctionMembers.ContainsKey((keyClass, keyFunction))) { _repo.FunctionMembers[(keyClass, keyFunction)] = new List <string>(); }
public override void display(CSemiExp semi) { Console.Write("\n line# {0}", repo_.semi.lineCount - 1); Console.Write("\n "); for (int i = 0; i < semi.count; ++i) { if (semi[i] != "\n" && !semi.isComment(semi[i])) { Console.Write("{0} ", semi[i]); } } }
public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectLeavingScope"); int index = semi.Contains("}"); if (index != -1) { doActions(semi); return(true); } return(false); }
public override void doAction(CSemiExp semi) { (var keyClass, var member) = (semi[0], semi[1]); if (!_repo.ClassMembers.ContainsKey(keyClass)) { _repo.ClassMembers[keyClass] = new List <string>(); } if (!_repo.ClassMembers[keyClass].Contains(member)) { _repo.ClassMembers[keyClass].Add(member); } }
public override void doAction(CSemiExp semi) { (var keyClass, var dependencyClass) = (semi[0], semi[1]); if (!_repo.ClassDependencies.ContainsKey(keyClass)) { _repo.ClassDependencies[keyClass] = new List <string>(); } if (!_repo.ClassDependencies[keyClass].Contains(dependencyClass)) { _repo.ClassDependencies[keyClass].Add(dependencyClass); } }
public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectNamespace"); int index = semi.Contains("namespace"); if (index != -1) { CSemiExp local = new CSemiExp(); // create local semiExp with tokens for type and name local.displayNewLines = false; local.Add(semi[index]).Add(semi[index + 1]); doActions(local); return(true); } return(false); }
//extract the type out of the expression public override void doAction(CSemiExp semi) { int classIndex = semi.Contains("class"); int structIndex = semi.Contains("struct"); if (classIndex != -1) { UserType.getUserDefinedSet().Add(semi[classIndex + 1]); string key = semi[classIndex + 1]; } else if (structIndex != -1) { string key = semi[structIndex + 1]; UserType.getUserDefinedSet().Add(semi[structIndex + 1]); } }
public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectAnonymousScope"); int index = semi.Contains("{"); if (index != -1) { CSemiExp local = new CSemiExp(); // create local semiExp with tokens for type and name local.displayNewLines = false; local.Add("control").Add("anonymous"); doActions(local); return(true); } return(false); }
public override void doAction(CSemiExp semi) { Elem elem; try { elem = repo_.stack.pop(); Display.Display.displayActions(actionDelegate, $"action PopStack ({elem.type}-{elem.name})"); for (int i = 0; i < repo_.locations.Count; ++i) { Elem temp = repo_.locations[i]; if (elem.type == temp.type) { if (elem.name == temp.name) { if ((repo_.locations[i]).endLine == 0) { (repo_.locations[i]).endLine = repo_.semi.lineCount; (repo_.locations[i]).endScopeCount = repo_.scopeCount; break; } } } } } catch { return; } CSemiExp local = new CSemiExp(); local.Add(elem.type).Add(elem.name); if (local[0] == "control") { return; } if (displaySemi) { Console.Write("\n line# {0,-5}", repo_.semi.lineCount); Console.Write("leaving "); string indent = new string(' ', 2 * (repo_.stack.count + 1)); Console.Write("{0}", indent); display(local); // defined in abstract action } }
public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectClass"); int indexCL = semi.Contains("class"); int indexIF = semi.Contains("interface"); int indexST = semi.Contains("struct"); int index = Math.Max(indexCL, indexIF); index = Math.Max(index, indexST); if (index != -1) { CSemiExp local = new CSemiExp(); local.displayNewLines = false; local.Add(semi[index]).Add(semi[index + 1]); doActions(local); return(true); } return(false); }
public override bool test(CSemiExp semi) { var accessModifiers = new List <string> { "public", "protected", "private", "internal" }; var mutationModifier = new List <string> { "readonly", "const" }; var currentClassName = _repo.stack.GetContext(e => e.type == "class")?.name; if (currentClassName == null) { // We're not inside a class return(false); } var containsAccessModifier = accessModifiers.Any(a => semi.Contains(a) != -1); var containsParens = semi.Contains("(") != -1 || semi.Contains(")") != -1; if (containsAccessModifier && !containsParens) { var local = new CSemiExp(); local.Add(currentClassName); if (semi.Contains("=") == -1) { local.Add(semi[semi.count - 2]); doActions(local); return(true); } else { var index = semi.FindFirst("="); local.Add(semi[index - 1]); doActions(local); return(true); } } return(false); }
public override void doAction(CSemiExp semi) { Display.Display.displayActions(actionDelegate, "action SaveDeclar"); Elem elem = new Elem(); elem.type = semi[0]; // expects type elem.name = semi[1]; // expects name elem.beginLine = repo_.semi.lineCount; elem.endLine = elem.beginLine; elem.beginScopeCount = repo_.scopeCount; elem.endScopeCount = elem.beginScopeCount; if (displaySemi) { Console.Write("\n line# {0,-5}", repo_.semi.lineCount - 1); Console.Write("entering "); string indent = new string(' ', 2 * repo_.stack.count); Console.Write("{0}", indent); display(semi); // defined in abstract action } repo_.locations.Add(elem); }
public override bool test(CSemiExp semi) { Display.Display.displayRules(actionDelegate, "rule DetectFunction"); if (semi[semi.count - 1] != "{") { return(false); } int index = semi.FindFirst("("); if (index > 0 && !isSpecialToken(semi[index - 1])) { var currentClassName = _repo.stack.GetContext(e => e.type == "class")?.name; CSemiExp local = new CSemiExp(); local .Add("function") .Add(semi[index - 1]) .Add(currentClassName); doActions(local); return(true); } return(false); }
public override void doAction(CSemiExp semi) { Display.Display.displayActions(actionDelegate, $"action PushStack ({semi[0]}-{semi[1]})"); ++repo_.scopeCount; Elem elem = new Elem(); elem.type = semi[0]; // expects type elem.name = semi[1]; // expects name if (semi.count > 2) { // expects class in which function resides or null if not inside a class elem.functionClass = semi[2]; } elem.beginLine = repo_.semi.lineCount - 1; elem.endLine = 0; elem.beginScopeCount = repo_.scopeCount; elem.endScopeCount = 0; repo_.stack.push(elem); if (displayStack) { repo_.stack.display(); } if (displaySemi) { Console.Write("\n line# {0,-5}", repo_.semi.lineCount - 1); Console.Write("entering "); string indent = new string(' ', 2 * repo_.stack.count); Console.Write("{0}", indent); display(semi); // defined in abstract action } if (elem.type == "control" || elem.name == "anonymous") { return; } repo_.locations.Add(elem); }
public override void doAction(CSemiExp semi) { display(semi); }
public override bool test(CSemiExp semi) { HashSet <string> definedSet = UserType.userDefinedSet; string[] specialTokens = { "get", "set" }; Display.displayFiles(actionDelegate, "rule Detect Association"); int publicIndex = semi.Contains("public"); int staticIndex = semi.Contains("static"); int privateIndex = semi.Contains("private"); int protectedIndex = semi.Contains("protected"); //ignoreing special words as aggregation for (int i = 0; i < specialTokens.Length; i++) { if (semi.Contains(specialTokens[i]) != -1) { return(false); } } //edge case for a single semi colon if (semi.count == 1 && semi[0].Equals(";")) { return(false); } //these characteristics is probably a function call rather than a declaration if (semi.Contains(".") != -1 && semi.Contains("(") != -1) { return(false); } if (semi.Contains("class") == -1 && semi.Contains("interface") == -1) //not class //make sure it is not a function { if (semi.Contains("(") != -1 && semi.Contains(";") == -1) { return(false); } //parse for the index of the declaration type int typeIndex = 0; //the default type index is 0 if no special variables //the coupling might datastructure related if (semi.Contains("<") != -1) { typeIndex = semi.Contains("<") + 1; } else if (staticIndex != -1) { typeIndex = staticIndex + 1; } else if (publicIndex != -1 || privateIndex != -1 || protectedIndex != -1) { int privacyIndex = Math.Max(Math.Max(publicIndex, privateIndex), protectedIndex); typeIndex = privacyIndex + 1; } else { for (int i = 0; i < semi.count; i++) { if (UserType.userDefinedSet.Contains(semi[i])) { typeIndex = i; break; } } } //keep track of the declaration type string declarationType = semi[typeIndex]; //store all data type and data member in repo for cohesion use Repository repository = Repository.getInstance(); if (repository.currentClassScope != null && repository.currentClassScope != "") { CSsemi.CSemiExp local = new CSemiExp(); local.Add(declarationType); local.Add(semi[typeIndex + 1]); if (repository.currentFunctionScope == null || repository.currentFunctionScope.Equals("")) { //since we are not in a function scope we can just store the class data members storeClassDataMember(repository, local); } else { //we are in a function scope so process the varibales in the function for cohesion processFunctionCohesion(repository, local); } } //only do action if this is user defined type for coupling if (UserType.userDefinedSet.Contains(declarationType)) { CSsemi.CSemiExp local = new CSemiExp(); local.Add(declarationType); if (semi.Contains("new") != -1) { local.Add("new"); } doActions(local); return(true); } } return(false); }
public RulesBasedParserTwo(CSemiExp semi, List <Elem> locations, Dictionary <string, List <string> > classMembers) { _locations = locations; _classMembers = classMembers; Repo.semi = semi; }
public RulesBasedParserOne(CSemiExp semi) { Repo.semi = semi; }
public static IEnumerable <ElementProperty> ParseFiles(IEnumerable <string> files, MIndexOptions mIndexOptions = null) { var firstPassLocations = new List <Elem>(); var dependencies = new Dictionary <string, List <string> >(); var classMembers = new Dictionary <string, List <string> >(); var functionMembers = new Dictionary <(string Class, string Method), List <string> >(); foreach (var file in files) { CSemiExp semi = new CSemiExp(); semi.displayNewLines = false; if (!semi.open(file)) { throw new ArgumentException($"Cannot open file {file}"); } var firstPassCodeAnalyzer = new RulesBasedParserOne(semi); RulesBasedParser firstPassParser = firstPassCodeAnalyzer.build(); while (semi.getSemi()) { firstPassParser.parse(semi); } semi.close(); var firstRepo = firstPassCodeAnalyzer?.GetRepository(); firstPassLocations.AddRange(firstRepo.locations); foreach (var member in firstRepo.ClassMembers) { classMembers[member.Key] = member.Value; } } foreach (var file in files) { CSemiExp semi = new CSemiExp(); semi.displayNewLines = false; if (!semi.open(file)) { throw new ArgumentException($"Cannot open file {file}"); } var secondPassCodeAnalyzer = new RulesBasedParserTwo(semi, firstPassLocations, classMembers); RulesBasedParser secondPassParser = secondPassCodeAnalyzer.build(); while (semi.getSemi()) { secondPassParser.parse(semi); } semi.close(); var secondRepo = secondPassCodeAnalyzer?.GetRepository(); foreach (var dependency in secondRepo.ClassDependencies) { dependencies[dependency.Key] = dependency.Value; } foreach (var member in secondRepo.FunctionMembers) { functionMembers[member.Key] = member.Value; } } var elements = firstPassLocations .Select(e => new ElementProperty { Type = e.type, Name = e.name, FunctionClass = e.functionClass, LinesOfCode = e.endLine - e.beginLine, CyclomaticComplexity = e.endScopeCount - e.beginScopeCount + 1 }) .ToList(); var newElements = new List <ElementProperty>(); foreach (var e in elements) { if (e.Type == "class") { // Class members if (classMembers.ContainsKey(e.Name)) { e.Members = classMembers[e.Name]; } // Class dependencies if (dependencies.ContainsKey(e.Name)) { foreach (var d in dependencies[e.Name]) { var element = elements.First(el => el.Name == d); e.Dependencies.Add(element); } } } else if (e.Type == "function" && functionMembers.ContainsKey((e.FunctionClass, e.Name))) { e.Members = functionMembers[(e.FunctionClass, e.Name)];
public override void doAction(CSemiExp semi) { Console.Write("\n line# {0}", repo_.semi.lineCount - 1); display(semi); }