public static void ParseNode(CsNode pNode, CodeBuilder pSb, FactoryExpressionCreator pCreator) { CsBlock block = pNode as CsBlock; if (block != null) { Parse(block, pSb, pCreator); return; } CsStatement statement = pNode as CsStatement; if (statement != null) { pSb.Indent(); parseStatement(statement, pSb, pCreator); pSb.Unindent(); return; } CsExpression expression = pNode as CsExpression; if (expression != null) { Expression ex = pCreator.Parse(pNode as CsExpression); pSb.Append(ex.Value + ";"); pSb.AppendLine(); return; } throw new Exception(); }
public static string GetType(CsNode pDirective) { if (pDirective == null) { return(""); } if (pDirective is CsNamespaceOrTypeName) { return(GetType((CsNamespaceOrTypeName)pDirective)); } if (pDirective is CsUsingNamespaceDirective) { return(GetType((CsUsingNamespaceDirective)pDirective)); } if (pDirective is CsTypeRef) { return(GetType((CsTypeRef)pDirective)); } if (pDirective is CsUsingAliasDirective) { return(GetType((CsUsingAliasDirective)pDirective)); } throw new Exception("Unsupported node type"); }
public MemberResult ProcessRoot(CsNode root) { switch (root.e) { case cs_node.n_block: var block = (CsBlock)root; foreach (var statement in block.statements) { ProcessStatement(statement); } break; case cs_node.n_expression_statement: ProcessStatement((CsStatement)root); break; default: Debugger.Break(); break; } return(result); }
private void tarjan(CsNode <String, String> node) // tarjan's algorithm { if (node.visited) { prev = node; return; } node.low_num = node.ID; // graph.seen.Push(node); node.visited = true; foreach (CsEdge <String, String> child in node.children) { tarjan(child.targetNode); //Console.WriteLine(graph.seen.Contains(prev)); if (graph.seen.Contains(prev)) { node.low_num = Math.Min(node.low_num, prev.low_num); } } if (node.low_num == node.ID) { while (graph.seen.Pop() != node) { } } prev = node; return; }
public static TheClass Get(CsNode pNode, FactoryExpressionCreator pCreator) { if (pNode == null) { return(null); } CsExpression csExpression = pNode as CsExpression; if (csExpression != null && csExpression.ec != expression_classification.ec_nothing) { return(Get((CsEntity)csExpression.entity, pCreator)); } while (pNode != null) { if (pNode is CsTypeRef || pNode is CsClass || pNode is CsInterface) { break; } pNode = pNode.parent; } CsClass klass = pNode as CsClass; if (klass != null) { if (!_classes.ContainsKey(klass)) { _classes[klass] = new TheClass(klass, pCreator); } return(_classes[klass]); } CsTypeRef csTypeRef = pNode as CsTypeRef; if (csTypeRef != null) { return(csTypeRef.entity_typeref == null ? null : Get((CsEntityClass)(csTypeRef.entity_typeref.u), pCreator)); } CsInterface csInterface = pNode as CsInterface; if (csInterface != null) { if (!_interfaces.ContainsKey(csInterface)) { _interfaces[csInterface] = new TheClass(csInterface, pCreator); } return(_interfaces[csInterface]); } throw new Exception(); }
// build a graph with the file dependency table public void buildgraph(Dictionary <String, HashSet <String> > depTable) { foreach (KeyValuePair <String, HashSet <String> > entry in depTable) { graph.addNode(new CsNode <string, string>(entry.Key)); } foreach (KeyValuePair <String, HashSet <String> > entry in depTable) { CsNode <String, String> node1 = find(entry.Key); foreach (String file in entry.Value) { node1.addChild(find(file), "null"); } } }
private void ProcessLambdaExpressionBody(CsNode csNode) { switch (csNode.e) { case cs_node.n_primary_expression_member_access: var memberAccess = (CsPrimaryExpressionMemberAccess)csNode; var memberResult = ProcessIdentifier(memberAccess.identifier); result = memberResult; break; default: Debugger.Break(); break; } }
private MemberResult ProcessResultExpression(CsNode node) { switch (node.e) { case cs_node.n_simple_name: var simpleName = (CsSimpleName)node; var memberResult = ProcessSimpleName(simpleName); result = memberResult; break; default: Debugger.Break(); break; } return(null); }
public void ProcessArg(CsNode arg, string code) { switch (arg.e) { case cs_node.n_block: var block = (CsBlock)arg; foreach (var statement in block.statements) { ProcessStatement(statement); } break; default: Debugger.Break(); break; } }
public static TheClass Get(CsNode pNode, FactoryExpressionCreator pCreator) { if (pNode == null) return null; CsExpression csExpression = pNode as CsExpression; if (csExpression != null && csExpression.ec != expression_classification.ec_nothing) { return Get((CsEntity)csExpression.entity, pCreator); } while (pNode != null) { if (pNode is CsTypeRef || pNode is CsClass || pNode is CsInterface) { break; } pNode = pNode.parent; } CsClass klass = pNode as CsClass; if (klass != null) { if (!_classes.ContainsKey(klass)) _classes[klass] = new TheClass(klass, pCreator); return _classes[klass]; } CsTypeRef csTypeRef = pNode as CsTypeRef; if (csTypeRef != null) { return csTypeRef.entity_typeref == null ? null : Get((CsEntityClass)(csTypeRef.entity_typeref.u), pCreator); } CsInterface csInterface = pNode as CsInterface; if (csInterface != null) { if (!_interfaces.ContainsKey(csInterface)) _interfaces[csInterface] = new TheClass(csInterface, pCreator); return _interfaces[csInterface]; } throw new Exception(); }
//public static void ParseBlockOrStatementOrExpression(CsNode pNode, CodeBuilder pSb, FactoryExpressionCreator pCreator) { // CsBlock block = pNode as CsBlock; // if (block != null) { // Parse(block, pSb, pCreator); // return; // } // CsStatement statement = pNode as CsStatement; // if (statement != null) { // pSb.Indent(); // parseStatement(statement, pSb, pCreator); // pSb.Unindent(); // return; // } // Expression ex = pCreator.Parse(pNode as CsExpression); // pSb.Append(ex.Value+";"); // pSb.AppendLine(); //} private static string parseNode(CsNode pNode, FactoryExpressionCreator pCreator) { Expression ex = pCreator.Parse(pNode as CsExpression); return ex.Value; }
//public static void ParseBlockOrStatementOrExpression(CsNode pNode, CodeBuilder pSb, FactoryExpressionCreator pCreator) { // CsBlock block = pNode as CsBlock; // if (block != null) { // Parse(block, pSb, pCreator); // return; // } // CsStatement statement = pNode as CsStatement; // if (statement != null) { // pSb.Indent(); // parseStatement(statement, pSb, pCreator); // pSb.Unindent(); // return; // } // Expression ex = pCreator.Parse(pNode as CsExpression); // pSb.Append(ex.Value+";"); // pSb.AppendLine(); //} private static string parseNode(CsNode pNode, FactoryExpressionCreator pCreator) { Expression ex = pCreator.Parse(pNode as CsExpression); return(ex.Value); }
private Type ProcessResultExpression(CsNode node) { switch (node.e) { case cs_node.n_simple_name: var simpleName = (CsSimpleName)node; var memberResult = ProcessSimpleName(simpleName); methodGenerator.Emit(OpCodes.Ldarg_1); if (memberResult.IsField) { if (memberResult.IsPublic) { methodGenerator.Emit(OpCodes.Ldfld, memberResult.Field); } else { CallGetPrivateField(memberResult); } if (memberResult.IsBoxType) { methodGenerator.Emit(OpCodes.Box, memberResult.Type); } return(memberResult.Type); } else if (memberResult.IsProperty) { if (memberResult.IsPublic) { var property = memberResult.Property; var method = property.GetGetMethod(); methodGenerator.EmitCall(OpCodes.Callvirt, method, null); } else { CallGetPrivateProperty(memberResult); } if (memberResult.IsBoxType) { methodGenerator.Emit(OpCodes.Box, memberResult.Type); } return(memberResult.Type); } break; case cs_node.n_invocation_expression: default: Debugger.Break(); break; } return(null); }
public static string GetType(CsNode pDirective) { if (pDirective == null) { return ""; } if (pDirective is CsNamespaceOrTypeName) { return GetType((CsNamespaceOrTypeName)pDirective); } if (pDirective is CsUsingNamespaceDirective) { return GetType((CsUsingNamespaceDirective)pDirective); } if (pDirective is CsTypeRef) { return GetType((CsTypeRef)pDirective); } if (pDirective is CsUsingAliasDirective) { return GetType((CsUsingAliasDirective)pDirective); } throw new Exception("Unsupported node type"); }
private void BuildNodeTree(GitHubFile file, int?nodeId = null) { int currentBracketCount = 0; bool childNodeGenerationStarted = false; CsNode thisNode = new CsNode(); if (nodeId == null) { thisNode.ParentNode = null; thisNode.ParentNodeId = null; thisNode.FileId = file.FileId; thisNode.File = file; thisNode.NodeName = "File"; thisNode.LineIds = new List <int>(); thisNode.ChildNodeIds = new List <int>(); List <GitHubLine> linesInFile; lock (thisLock) { linesInFile = _context.GitHubLines.Where(l => l.FileId == file.FileId).ToList(); } foreach (GitHubLine l in linesInFile) { thisNode.LineIds.Add(l.LineInFile); } thisNode.LineIds = thisNode.LineIds.OrderBy(l => l).ToList(); lock (thisLock) { _context.CsNodes.Add(thisNode); _context.SaveChanges(); } } else { currentBracketCount = -1; lock (thisLock) { thisNode = _context.CsNodes.Where(n => n.Id == nodeId).Single(); } } List <GitHubLine> fileLines; lock (thisLock) { fileLines = _context.GitHubLines.Where(l => l.FileId == file.FileId).OrderBy(l => l.LineInFile).ToList(); } CsNode childNode = new CsNode { NodeName = "", ParentNodeId = thisNode.Id, ParentNode = thisNode, LineIds = new List <int>(), FileId = file.FileId, File = file, ChildNodeIds = new List <int>(), }; int firstLine = thisNode.LineIds.Min(i => i); int lastLine = thisNode.LineIds.Max(i => i); for (int i = firstLine - 1; i < lastLine; i++) { string thisLine = fileLines[i].Content; string nextLine = null; if (i + 1 < fileLines.Count) { nextLine = fileLines[i + 1].Content; } else { nextLine = ""; } if ((thisLine.Contains("namespace") || thisLine.Contains("class") || (thisLine.Contains('(') && thisLine.Contains(')') && !thisLine.Contains(';'))) && currentBracketCount == 0 && childNodeGenerationStarted == false) { childNode = new CsNode(); childNodeGenerationStarted = true; if (thisLine.Contains("namespace")) { childNode.NodeName = "Namespace"; } else if (thisLine.Contains("class")) { childNode.NodeName = "Class"; } else if (thisLine.Contains('(') && thisLine.Contains(')') && !thisLine.Contains(';')) { childNode.NodeName = "Method"; } else { childNode.NodeName = "Error"; } childNode.ParentNodeId = thisNode.Id; childNode.ParentNode = thisNode; childNode.LineIds = new List <int>(); childNode.FileId = file.FileId; childNode.File = file; childNode.ChildNodeIds = new List <int>(); childNode.LineIds.Add(fileLines[i].LineInFile); if (thisLine.Contains('{')) { currentBracketCount++; } } else { if (childNodeGenerationStarted == true) { childNode.LineIds.Add(fileLines[i].LineInFile); } if (thisLine.Contains('{')) { currentBracketCount++; } else if (thisLine.Contains('}')) { currentBracketCount--; } if (currentBracketCount == 0 && childNodeGenerationStarted == true) { childNodeGenerationStarted = false; lock (thisLock) { _context.CsNodes.Add(childNode); _context.SaveChanges(); thisNode = _context.CsNodes.Where(n => n.Id == thisNode.Id).Single(); thisNode.ChildNodeIds.Add(childNode.Id); _context.CsNodes.Update(thisNode); _context.SaveChanges(); } } } } foreach (int childNodeId in thisNode.ChildNodeIds) { CsNode thisChildNode = _context.CsNodes.Where(n => n.Id == childNodeId).Single(); BuildNodeTree(file, thisChildNode.Id); } }