public void ProcessAssembly(string aName, Encoding encode) { TextReader reader = new StreamReader(aName, encode); StringBuilder sb = new StringBuilder(); sb.Append(reader.ReadToEnd()); reader.Close(); reader = new StreamReader(aName, encode); LZ.Lexer.CPP.Lexer lex = new LZ.Lexer.CPP.Lexer(reader); string[] list = sb.ToString().Split('\n'); LZ.Lexer.Token tokenFirst = null, token = null; int i = 0; do { i++; token = lex.NextToken(); if (token.kind == 0) break; if (tokenFirst == null) { tokenFirst = token; } Debug.WriteLine(string.Format("{0}. {1}", i, GetTokenText(list, token))); } while (token.kind != 0); tokenCurrent = null; this._work = new Stack(); tokenCurrent = tokenFirst; string sModifier = "private"; // 0=private;1=protected;2=public; while (tokenCurrent != null && tokenCurrent.next != null) { string line = list[tokenCurrent.line - 1]; int nLen = tokenCurrent.EndLocation.X - tokenCurrent.Location.X; string word = line.Substring(tokenCurrent.Location.X - 1, nLen); TypeInfo ti = null; switch (word) { case "class": sModifier = "private"; ProcessAType(ref ti, ref sModifier, list, false); if (ti.IsClass) AddClassifier(ti, true); ////if (!this._work.Contains(ti)) ////{ //// this._work.Push(ti); ////} break; case "struct": sModifier = "public"; ProcessAType(ref ti, ref sModifier, list, true); if (ti.IsClass) AddClassifier(ti, true); break; default: break; } ////this._work = new Stack(); ////foreach (Type type in types) ////{ //// if (!this._work.Contains(type)) //// { //// this._work.Push(type); //// } ////} ////this.ProcessWork(); tokenCurrent = tokenCurrent.next; } reader.Close(); ////Assembly assembly; ////try ////{ //// assembly = Assembly.LoadFrom(aName); //// LZ.Reverse.Info.Write(new string[] { "... processing assembly \"{0}\"", aName }); ////} ////catch (FileNotFoundException) ////{ //// LZ.Reverse.Info.Write(new string[] { "*** could not load Assembly: \"{0}\"", aName }); //// return; ////} ////catch (FileLoadException) ////{ //// LZ.Reverse.Info.Write(new string[] { "*** could not load Assembly: \"{0}\"", aName }); //// return; ////} ////catch (Exception) ////{ //// LZ.Reverse.Info.Write(new string[] { "*** could not load Assembly: \"{0}\"", aName }); //// return; ////} ////if (this._mdl.Comment == "") ////{ //// this._mdl.Comment = "Reversed assemblies:\r\n"; ////} ////this._mdl.Comment = string.Concat(new object[] { this._mdl.Comment, aName, " (", assembly.GetName(), ")\r\n" }); ////Type[] types = new Type[0]; ////try ////{ //// if (this._bAllTypes) //// { //// try //// { //// types = assembly.GetTypes(); //// } //// catch (ReflectionTypeLoadException exception) //// { //// if (LZ.Reverse.Info._bDebug) //// { //// LZ.Reverse.Info.Write(new string[] { "*** exception accessing the list of all types, replaced by the list of exported types \"{0}\"", exception.ToString() }); //// } //// types = assembly.GetExportedTypes(); //// if (this._recur == Recursion.NONE) //// { //// this._recur = Recursion.LIMITED; //// } //// } //// } //// else //// { //// types = assembly.GetExportedTypes(); //// } ////} ////catch (Exception exception2) ////{ //// if (LZ.Reverse.Info._bDebug) //// { //// LZ.Reverse.Info.Write(new string[] { "*** exception during type enumeration \"{0}\"", exception2.ToString() }); //// } ////} ////this._work = new Stack(); ////foreach (Type type in types) ////{ //// if (!this._work.Contains(type)) //// { //// this._work.Push(type); //// } ////} ////this.ProcessWork(); }
public void ProcessAFile(string aName, Encoding encode) { Debug.WriteLine(string.Format("reverse file: {0}", aName)); TextReader reader = new StreamReader(aName, encode); _lexer = new LZ.Lexer.CPP.Lexer(reader); LZ.Lexer.Token tokenFirst = null, token = null; int i = 0; do { i++; token = _lexer.NextToken(); if (token.kind == 0) break; if (tokenFirst == null) { tokenFirst = token; } //Debug.WriteLine(string.Format("{0}. {1}", i, GetTokenText(token))); } while (token.kind != 0); tokenCurrent = null; tokenCurrent = tokenFirst; string sModifier = "private"; // 0=private;1=protected;2=public; bool isInTypeDef = false, isParameter = false, isGenericType = false; while (tokenCurrent != null && tokenCurrent.next != null) { string word = tokenCurrent.val; TypeInfo ti = null; switch (word) { case "template": isGenericType = true; ReadTemplate(); break; case "class": sModifier = "private"; ti = new TypeInfo(); ti.Comment = aName; ti.IsGenericType = isGenericType; ProcessAType(ref ti, ref sModifier, false); isGenericType = false; ////if (ti.IsClass) AddClassifier(ti, true); if (!this._work.Contains(ti)) { this._work.Push(ti); } break; case "struct": // 跳过在类型定义体中的struct关键字 if (isParameter) break; ti = new TypeInfo(); ti.Comment = aName; sModifier = "public"; ti.IsValueType = true; ti.IsGenericType = isGenericType; ProcessAType(ref ti, ref sModifier, true); isGenericType = false; if (ti.IsClass && !this._work.Contains(ti)) { this._work.Push(ti); } ////if (ti.IsClass) AddClassifier(ti, true); break; case "typedef": isInTypeDef = true; break; case ";": isInTypeDef = false; break; case "(": isParameter = true; break; case ")": isParameter = false; break; default: break; } if (tokenCurrent == null) break; tokenCurrent = tokenCurrent.next; } // 关闭文件 reader.Close(); _lexer = null; }