public override bool test(CSsemi.CSemiExp semi) { HashSet <string> definedSet = UserType.getUserDefinedSet(); Display.displayRules(actionDelegate, "rule Detect Inheritance"); int classIndex = semi.Contains("class"); if (classIndex != -1) { //check for inheritance int inheritanceIndex = semi.Contains(":"); if (inheritanceIndex != -1) { string inheritedClass = semi[inheritanceIndex + 1]; if (definedSet.Contains(inheritedClass)) { //do some action Console.Write("inheritance found: " + inheritedClass + "\n"); doActions(semi); return(true); } } } return(false); }
public override bool test(CSsemi.CSemiExp semi) { 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) { CSsemi.CSemiExp local = new CSsemi.CSemiExp(); // local semiExp with tokens for type and name local.displayNewLines = false; //check for inheritance if (semi.Contains(":") != -1) { local.userInheritance = true; local.Add(semi[index]).Add(semi[index + 1]).Add(semi[semi.Contains(":") + 1]); } else { local.Add(semi[index]).Add(semi[index + 1]); } doActions(local); return(true); } return(false); }
public override void doAction(CSsemi.CSemiExp semi) { Display.displayActions(actionDelegate, "action PushStack"); ++repo_.scopeCount; Elem elem = new Elem(); elem.type = semi[0]; // expects type elem.name = semi[1]; // expects name elem.beginLine = repo_.semi.lineCount - 1; elem.endLine = 0; elem.beginScopeCount = repo_.scopeCount; elem.endScopeCount = 0; repo_.stack.push(elem); if (AAction.displayStack) { repo_.stack.display(); } if (AAction.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); this.display(semi); // defined in abstract action } if (elem.type == "control" || elem.name == "anonymous") { return; } repo_.locations.Add(elem); }
////helper method to store all class data memebers in repo public void storeClassDataMember(Repository repository, CSsemi.CSemiExp local) { if (repository != null) { if (repository.currentClassScope != null && (repository.currentFunctionScope == "" || repository.currentFunctionScope == null)) { Dictionary <string, List <Elem> > locationsTable = repository.LocationsTable; Elem elem = new Elem(); elem.type = local[0]; elem.name = local[1]; if (locationsTable.ContainsKey(repository.currentClassScope)) { List <Elem> elements = locationsTable[repository.currentClassScope]; elements.Add(elem); locationsTable[repository.currentClassScope] = elements; } else { List <Elem> elements = new List <Elem>(); elements.Add(elem); locationsTable.Add(repository.currentClassScope, elements); } } } }
public override bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectClass"); int indexCL = semi.Contains("class"); int indexIF = semi.Contains("interface"); int indexST = semi.Contains("struct"); int indexEN = semi.Contains("enum"); int index = Math.Max(indexCL, indexIF); index = Math.Max(index, indexST); index = Math.Max(index, indexEN); if (index != -1) { CSsemi.CSemiExp local = new CSsemi.CSemiExp(); // local semiExp with tokens for type and name local.displayNewLines = false; if (semi[index] == "class" || semi[index] == "interface" || semi[index] == "struct" || semi[index] == "enum") { local.Add(semi[index]).Add(semi[index + 1]); } else { local.Add(semi[index]).Add(semi[index + 2]); } doActions(local); return(true); } return(false); }
//----< collect semiExpression from filtered token stream >---------- public bool getSemi() { semiExp.RemoveRange(0, semiExp.Count); // empty container do { get(); if (currTok == "") { return(false); // end of file } if (returnNewLines || currTok != "\n") { semiExp.Add(currTok); } } while (!isTerminator(currTok) || count == 0); // if for then append next two semiExps, e.g., for(int i=0; i<se.count; ++i) { if (semiExp.Contains("for")) { CSemiExp se = clone(); getSemi(); se.Add(semiExp.ToArray()); getSemi(); se.Add(semiExp.ToArray()); semiExp.Clear(); for (int i = 0; i < se.count; ++i) { semiExp.Add(se[i]); } } return(semiExp.Count > 0); }
public void doActions(CSsemi.CSemiExp semi) { foreach (IAction action in actions) { action.doAction(semi); } }
public override bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectPublicDeclar"); int index = semi.Contains(";"); if (index != -1) { index = semi.Contains("public"); if (index == -1) { return(true); } CSsemi.CSemiExp local = new CSsemi.CSemiExp(); // create local semiExp with tokens for type and name 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(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectFunction"); if (semi[semi.count - 1] != "{") { return(false); } int index = semi.FindFirst("("); if (index > 0 && !isSpecialToken(semi[index - 1])) { CSsemi.CSemiExp local = new CSsemi.CSemiExp(); local.Add("function").Add(semi[index - 1]); //search for all arguments in the function and add to local for (int i = index; i < semi.count; i++) { if (UserType.userDefinedSet.Contains(semi[i])) { local.Add(semi[i]); } } doActions(local); return(true); } return(false); }
static void Main(string[] args) { Console.Write("\n Testing semiExp Operations"); CSemiExp test = new CSemiExp(); test.returnNewLines = true; test.displayNewLines = true; /* If the package is run as stand alone application * then add the default values for the member variables */ try { string testFile = "Parser.cs"; if (!test.open(testFile)) { Console.Write("\n Can't open file {0}", testFile); } while (test.getSemi()) { test.display(); } test.initialize(); test.insert(0, "this"); test.insert(1, "is"); test.display(); Console.Write("\n removing first token:"); test.remove(0); test.display(); Console.Write("\n removing token \"test\":"); test.remove("test"); test.display(); Console.Write("\n making copy of semiExpression:"); CSemiExp copy = test.clone(); copy.display(); if (args.Length == 0) { Console.Write("\n Please enter name of file to analyze\n\n"); return; } CSemiExp semi = new CSemiExp(); semi.returnNewLines = true; if (!semi.open(args[0])) { Console.Write("\n can't open file {0}\n\n", args[0]); return; } Console.Write("\n Analyzing file {0}", args[0]); while (semi.getSemi()) { semi.display(); } semi.close(); Console.ReadLine(); } catch { Console.WriteLine("Error in semi module"); } }
public override void doAction(CSsemi.CSemiExp semi) { Display.displayActions(actionDelegate, "action PushStack"); ++repo_.scopeCount; Elem elem = new Elem(); elem.type = semi[0]; // expects type elem.name = semi[1]; // expects name elem.beginLine = repo_.semi.lineCount - 1; elem.endLine = 0; elem.beginScopeCount = repo_.scopeCount; elem.endScopeCount = 0; elem.cohesion = 0; elem.coupling = 0; //keep track of the current class and function scope if (elem.name != "anonymous") { if (elem.type.Equals("class") || elem.type.Equals("struct") || elem.type.Equals("interface")) { repo_.currentClassScope = elem.name; //set the current class name } else if (elem.type.Equals("function")) { repo_.currentFunctionScope = elem.name; } } repo_.stack.push(elem); if (AAction.displayStack) { repo_.stack.display(); } if (AAction.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); this.display(semi); // defined in abstract action } if (elem.type == "control" || elem.name == "anonymous") { return; } repo_.locations.Add(elem); //update coupling base on class inhertiance on use if (semi.Contains("class") != -1) { inheritCoupling(semi); } else { userCoupling(semi); } }
// //----< make a copy of semiEpression >------------------------------- public CSemiExp clone() { CSemiExp copy = new CSemiExp(); for (int i = 0; i < count; ++i) { copy.Add(this[i]); } return(copy); }
public virtual void display(CSsemi.CSemiExp semi) { if (displaySemi) { for (int i = 0; i < semi.count; ++i) { Console.Write("{0} ", semi[i]); } } }
public override void display(CSsemi.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(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectLeavingScope"); int index = semi.Contains("}"); if (index != -1) { doActions(semi); return(true); } return(false); }
public override void doAction(CSsemi.CSemiExp semi) { Display.displayActions(actionDelegate, "action SaveDeclar"); Elem elem; try { elem = repo_.stack.pop(); //clear the function scope if we are exiting from it if (elem.name.Equals(repo_.currentFunctionScope)) { repo_.currentFunctionScope = ""; } 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; } CSsemi.CSemiExp local = new CSsemi.CSemiExp(); local.Add(elem.type).Add(elem.name); if (local[0] == "control") { return; } if (AAction.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); this.display(local); // defined in abstract action } }
//----< Test Stub >-------------------------------------------------- #if (TEST_PARSER) static void Main(string[] args) { Console.Write("\n Demonstrating Parser"); Console.Write("\n ======================\n"); ShowCommandLine(args); List <string> files = TestParser.ProcessCommandline(args); foreach (object file in files) { Console.Write("\n Processing file {0}\n", file as string); CSsemi.CSemiExp semi = new CSsemi.CSemiExp(); semi.displayNewLines = false; if (!semi.open(file as string)) { Console.Write("\n Can't open {0}\n\n", args[0]); return; } Console.Write("\n Type and Function Analysis"); Console.Write("\n ----------------------------\n"); BuildCodeAnalyzer builder = new BuildCodeAnalyzer(semi); Parser parser = builder.build(); try { while (semi.getSemi()) { parser.parse(semi); } Console.Write("\n\n locations table contains:"); } catch (Exception ex) { Console.Write("\n\n {0}\n", ex.Message); } Repository rep = Repository.getInstance(); List <Elem> table = rep.locations; foreach (Elem e in table) { Console.Write("\n {0,10}, {1,25}, {2,5}, {3,5}", e.type, e.name, e.begin, e.end); } Console.WriteLine(); Console.Write("\n\n That's all folks!\n\n"); semi.close(); Console.ReadLine(); } }
// get back the Tyeptable for other program using public TypeTable getTypeTable(string[] args) { TestParser tp = new TestParser(); TypeTable tt = new TypeTable(); string ns = ""; foreach (string file in args) { CSsemi.CSemiExp semi = new CSsemi.CSemiExp(); semi.displayNewLines = false; if (!semi.open(file as string)) { Console.Write("\n Can't open {0}\n\n", args[0]); } BuildCodeAnalyzer builder = new BuildCodeAnalyzer(semi); Parser parser = builder.build(); try { while (semi.getSemi()) { parser.parse(semi); } } catch (Exception ex) { Console.Write("\n\n {0}\n", ex.Message); } Repository rep = Repository.getInstance(); List <Elem> table = rep.locations; foreach (Elem e in table) { if (e.type == "namespace") { ns = e.name; } if (e.type == "interface" || e.type == "class" || e.type == "struct" || e.type == "enum" || e.type == "delegate") { tt.add(e.name, Path.GetFileName(file), ns); } } semi.close(); } return(tt); }
public void parse(CSsemi.CSemiExp semi) { // Note: rule returns true to tell parser to stop // processing the current semiExp Display.displaySemiString(semi.displayStr()); foreach (IRule rule in Rules) { if (rule.test(semi)) { break; } } }
public int indexOfType(CSsemi.CSemiExp semi) { int indexCL = semi.Contains("class"); int indexIF = semi.Contains("interface"); int indexST = semi.Contains("struct"); int indexEN = semi.Contains("enum"); int indexDE = semi.Contains("delegate"); int index = Math.Max(indexCL, indexIF); index = Math.Max(index, indexST); index = Math.Max(index, indexEN); index = Math.Max(index, indexDE); return(index); }
public void parse(CSsemi.CSemiExp semi) { // Note: rule returns true to tell parser to stop // processing the current semiExp foreach (IRule rule in Rules) { //semi.display(); if (rule.test(semi)) { break; } } {} }
public override bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetecAlias"); int indexF = semi.Contains("using"); int indexS = semi.Contains("="); if (indexF != -1 && indexS != -1 && (indexS - indexF) == 2) { CSsemi.CSemiExp local = new CSsemi.CSemiExp(); local.displayNewLines = false; local.Add("Alias").Add(semi[indexF + 1]); doActions(local); return(true); } return(false); }
public override bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectAnonymousScope"); int index = semi.Contains("{"); if (index != -1) { CSsemi.CSemiExp local = new CSsemi.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 bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectNamespace"); int index = semi.Contains("namespace"); if (index != -1) { CSsemi.CSemiExp local = new CSsemi.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); }
//----< test for equality >------------------------------------------ override public bool Equals(Object semi) { CSemiExp temp = (CSemiExp)semi; if (temp.count != this.count) { return(false); } for (int i = 0; i < temp.count && i < this.count; ++i) { if (this[i] != temp[i]) { return(false); } } return(true); }
public override bool test(CSsemi.CSemiExp semi) { Display.displayRules(actionDelegate, "rule DetectFunction"); if (semi[semi.count - 1] != "{") { return(false); } int index = semi.FindFirst("("); if (index > 0 && !isSpecialToken(semi[index - 1])) { CSsemi.CSemiExp local = new CSsemi.CSemiExp(); local.Add("function").Add(semi[index - 1]); doActions(local); return(true); } return(false); }
public override void doAction(CSsemi.CSemiExp semi) { 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 (AAction.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); this.display(semi); // defined in abstract action } repo_.locations.Add(elem); }
static void Main(string[] args) { Console.Write("\n Testing semiExp Operations"); Console.Write("\n ============================\n"); CSemiExp test = new CSemiExp(); test.returnNewLines = true; test.displayNewLines = true; string testFile = "Parser.cs"; if (!test.open(testFile)) { Console.Write("\n Can't open file {0}", testFile); } while (test.getSemi()) { test.display(); } test.initialize(); test.insert(0, "this"); test.insert(1, "is"); test.insert(2, "a"); test.insert(3, "test"); test.display(); Console.Write("\n 2nd token = \"{0}\"\n", test[1]); Console.Write("\n removing first token:"); test.remove(0); test.display(); Console.Write("\n"); Console.Write("\n removing token \"test\":"); test.remove("test"); test.display(); Console.Write("\n"); }
//helper method to update the coupling parameter //base on inheritance if conditions are met public void inheritCoupling(CSsemi.CSemiExp semi) { ///update class coupling if inheritance found if (semi.userInheritance) { if (UserType.userDefinedSet.Contains(semi[2])) { string classScope = repo_.currentClassScope; if (classScope != null) { for (int i = 0; i < repo_.locations.Count; i++) { Elem tempElem = repo_.locations[i]; if (tempElem.name.Equals(classScope)) { repo_.locations[i].coupling += 1; break; } } } } } }
//helper method to update coupling base on use public void userCoupling(CSsemi.CSemiExp semi) { for (int i = 0; i < semi.count; i++) { if (UserType.userDefinedSet.Contains(semi[i])) { string classScope = repo_.currentClassScope; string functionScope = repo_.currentFunctionScope; if (classScope != null) { for (int j = 0; j < repo_.locations.Count; j++) { Elem tempElem = repo_.locations[j]; if (tempElem.name.Equals(classScope) || tempElem.name.Equals(functionScope)) { repo_.locations[j].coupling += 1; //break; } } } } } }
//----< Test Stub >-------------------------------------------------- #if(TEST_PARSER) static void Main(string[] args) { Console.Write("\n Demonstrating Parser"); Console.Write("\n ======================\n"); ShowCommandLine(args); List<string> files = TestParser.ProcessCommandline(args); foreach (object file in files) { Console.Write("\n Processing file {0}\n", file as string); CSsemi.CSemiExp semi = new CSsemi.CSemiExp(); semi.displayNewLines = false; if (!semi.open(file as string)) { Console.Write("\n Can't open {0}\n\n", args[0]); return; } Console.Write("\n Type and Function Analysis"); Console.Write("\n ----------------------------\n"); BuildCodeAnalyzer builder = new BuildCodeAnalyzer(semi); Parser parser = builder.build(); try { while (semi.getSemi()) parser.parse(semi); Console.Write("\n\n locations table contains:"); } catch (Exception ex) { Console.Write("\n\n {0}\n", ex.Message); } Repository rep = Repository.getInstance(); List<Elem> table = rep.locations; foreach (Elem e in table) { Console.Write("\n {0,10}, {1,25}, {2,5}, {3,5}", e.type, e.name, e.begin, e.end); } Console.WriteLine(); Console.Write("\n\n That's all folks!\n\n"); semi.close(); Console.ReadLine(); } }
static void Main(string[] args) { Console.Write("\n Testing semiExp Operations"); Console.Write("\n ============================\n"); CSemiExp test = new CSemiExp(); test.returnNewLines = true; test.displayNewLines = true; string testFile = "Parser.cs"; if (!test.open(testFile)) Console.Write("\n Can't open file {0}", testFile); while (test.getSemi()) test.display(); test.initialize(); test.insert(0, "this"); test.insert(1, "is"); test.insert(2, "a"); test.insert(3, "test"); test.display(); Console.Write("\n 2nd token = \"{0}\"\n", test[1]); Console.Write("\n removing first token:"); test.remove(0); test.display(); Console.Write("\n"); Console.Write("\n removing token \"test\":"); test.remove("test"); test.display(); Console.Write("\n"); }
static void Main(string[] args) { Console.Write("\n Testing semiExp Operations"); Console.Write("\n ============================\n"); CSemiExp test = new CSemiExp(); test.returnNewLines = true; test.displayNewLines = true; string testFile = "../../testSemi.txt"; if(!test.open(testFile)) Console.Write("\n Can't open file {0}",testFile); while(test.getSemi()) test.display(); test.initialize(); test.insert(0,"this"); test.insert(1,"is"); test.insert(2,"a"); test.insert(3,"test"); test.display(); Console.Write("\n 2nd token = \"{0}\"\n",test[1]); Console.Write("\n removing first token:"); test.remove(0); test.display(); Console.Write("\n"); Console.Write("\n removing token \"test\":"); test.remove("test"); test.display(); Console.Write("\n"); Console.Write("\n making copy of semiExpression:"); CSemiExp copy = test.clone(); copy.display(); Console.Write("\n"); if(args.Length == 0) { Console.Write("\n Please enter name of file to analyze\n\n"); return; } CSemiExp semi = new CSemiExp(); semi.returnNewLines = true; if(!semi.open(args[0])) { Console.Write("\n can't open file {0}\n\n",args[0]); return; } Console.Write("\n Analyzing file {0}",args[0]); Console.Write("\n ----------------------------------\n"); while(semi.getSemi()) semi.display(); semi.close(); }
// //----< make a copy of semiEpression >------------------------------- public CSemiExp clone() { CSemiExp copy = new CSemiExp(); for (int i = 0; i < count; ++i) { copy.Add(this[i]); } return copy; }