internal static string RemovePreprocessoredOutCode(string namespaceContents) { if (mAddedDefines == null) { mAddedDefines = new List <string>(); mAddedDefines.Add("FRB_XNA"); } StringBuilder returnStringBuilder = new StringBuilder(); int index = 0; int bracketsDeep = 0; PreProcessorDefineParser.Clear(); string untrimmed; while (true) { string line = ""; untrimmed = GetLine(namespaceContents, ref index); if (untrimmed.EndsWith("\r")) { untrimmed = untrimmed.Substring(0, untrimmed.Length - 1); // we do this to get rid of the \r at the end } line = untrimmed.Trim(); if (line == "") { if (index != namespaceContents.Length) { continue; } else { break; } } else if (line.StartsWith("#if ") || line.StartsWith("#endif") || line.StartsWith("#elif") || line.StartsWith("#else")) { PreProcessorDefineParser.ParseLine(line); } else if (PreProcessorDefineParser.ShouldLineBeSkipped(mAddedDefines)) { continue; } else { returnStringBuilder.AppendLine(untrimmed); } } return(returnStringBuilder.ToString()); }
void ParseContents(string classContents, bool trim) { int index = 0; int bracketsDeep = 0; PreProcessorDefineParser.Clear(); string remainderOfLine = null; while (true) { string line = ""; string untrimmedLine = ""; if (remainderOfLine == null) { untrimmedLine = GetLine(classContents, ref index); line = untrimmedLine.Trim(); } else { line = remainderOfLine; } bool isComment = line.Trim().StartsWith("//"); remainderOfLine = null; #region If it's empty, continue or end depending on how far we are in the file if (line == "") { if (index != classContents.Length) { continue; } else { break; } } #endregion #region It's a define code like #if or #elif or #else else if (line.StartsWith("#if ") || line.StartsWith("#endif") || line.StartsWith("#elif") || line.StartsWith("#else")) { PreProcessorDefineParser.ParseLine(line); } #endregion else if (PreProcessorDefineParser.ShouldLineBeSkipped(mAddedDefines) || isComment) { continue; } #region It's just an empty line ( "\r" ) else if (line == "\r") { // do nothing, continue } #endregion #region It's an open bracket ( "{" ) else if (line.StartsWith("{")) { bracketsDeep++; if (mCurrentBlock.Count != 0) { if (trim) { AddToCurrentBlock(line, index); } else { AddToCurrentBlock(untrimmedLine, index); } } if (line != "{") { remainderOfLine = line.Substring(1); } } #endregion #region It's a close bracket ( "}" ) else if (line.StartsWith("}")) { bracketsDeep--; if (mCurrentBlock.Count != 0) { if (trim) { AddToCurrentBlock(line, index); } else { AddToCurrentBlock(untrimmedLine, index); } } if (bracketsDeep == 1) { ProcessCurrentBlock(index, trim); } if (line != "}") { remainderOfLine = line.Substring(1); } } #endregion #region If it's a field else if (bracketsDeep == 1 && line.EndsWith(";") && // C# 6 introduces assigning values like public float Something { get; set; } = 3; !line.Contains("{")) { if (line.Contains('(') && line.EndsWith(");") && !line.Contains("=")) { // likely a single-line method in an interface // void ClearRelationships(); if (trim) { AddToCurrentBlock(line, index); } else { AddToCurrentBlock(untrimmedLine, index); } ProcessCurrentBlock(index, trim); } else { ParseField(line); } } #endregion #region Attributes else if (bracketsDeep == 1 && line.StartsWith("[") && line.EndsWith("]")) { AddAttribute(line); } #endregion #region If there is a bracket inside a line "}" else if (bracketsDeep == 1 && NumberOfValid('{', line) != 0) { // Could be a single-liner // let's parse it as a property if (trim) { AddToCurrentBlock(line, index); } else { AddToCurrentBlock(untrimmedLine, index); } if (line.Contains('}')) { ProcessCurrentBlock(index, trim); } else { bracketsDeep++; } } #endregion else if (mCurrentBlock.Count == 0 && line.StartsWith("[") && line.EndsWith("]")) { // It's an attribute like [XmlIgnore]. Don't do anything with this for now } else if (line.StartsWith("#region ") || line.StartsWith("#endregion")) { // do nothing } #region Else, save off this line - it may be part of a method or property else { bool containsCloseBracket = false; if (bracketsDeep > 0) { if (trim) { AddToCurrentBlock(line, index); } else { AddToCurrentBlock(untrimmedLine, index); } } // The following methods will properly handle { and } inside quotes, like // string classFormat = "Level{0}"; if (NumberOfValid('{', line) > 0) { bracketsDeep++; } if (NumberOfValid('}', line) > 0) { bracketsDeep--; containsCloseBracket = true; } // I think we want to check this *before* increasing the bracketsDeep value //if (bracketsDeep > 0) //{ // if (trim) // { // AddToCurrentBlock(line, index); // } // else // { // AddToCurrentBlock(untrimmedLine, index); // } //} if (containsCloseBracket) { if (bracketsDeep == 1) { ProcessCurrentBlock(index, trim); } } } #endregion } // Mark any overloaded methods as such List <string> methodNamesSoFar = new List <string>(); for (int i = 0; i < mParsedMethods.Count - 1; i++) { ParsedMethod method = mParsedMethods[i]; for (int j = i + 1; j < mParsedMethods.Count; j++) { ParsedMethod otherMethod = mParsedMethods[j]; // Normally we use the Method.ToString to identify methods, but here we're looking for overloads, so we'll use name // ...but is this okay? Or do we want to use ToString to get exact matches? if (method.Name == otherMethod.Name) { method.IsOverload = true; otherMethod.IsOverload = true; } } } }