using System; using System.Text.RegularExpressions; using SU = Laj.StringUtils; namespace Laj.BabbelFisken { // Last modified: 2002-08-24 // Version: 0.49 ID:LAJ44007 // Compiles with Microsoft Visual C# .NET 7.0.9466 on Windows 95. // The source code is free. It is not under any kind of license. // If this code works, it was written by Lars Johansson, Göteborg. // If not, I don't know who wrote it. internal class VBNET { static public string[] DeleteSpaceUnderscoreAndSplit(string s) { s = s.Replace(" _" + Environment.NewLine, ""); return Regex.Split(s, Environment.NewLine); } static string getReturnType(string s) { s = Regex.Replace(s, @"\s+", " "); int indexBegin = s.LastIndexOf(") As "); if (indexBegin < 0) return ""; s = s.Substring(indexBegin+5); return s; } static string fixFunctionEnding(string s) { s = Regex.Replace(s, @"\s+", " "); int indexBegin = s.LastIndexOf(") As "); if (indexBegin < 0) return s + " {"; s = s.Substring(0, indexBegin+1); s = s + " {"; return s; } static string deleteAsAndSwap(string s) { string tokenPrevious, tokenNext; bool ok; int indexBeginTP, indexEndTP, indexBeginTN, indexEndTN; for(;;) //Repeat until no more " As " { int index = s.IndexOf(" As "); if (index < 0) return s; string stopString = "\t (,"; ok = SU.GetPreviousToken(s, index, stopString, out tokenPrevious, out indexBeginTP, out indexEndTP); if (!ok) return s; stopString = "\t ),"; ok = SU.GetNextToken(s, index+4, stopString, out tokenNext, out indexBeginTN, out indexEndTN); if (!ok) return s; s = SU.ReplaceInString(s, tokenPrevious, indexBeginTN, indexEndTN); s = s.Remove(index, 3); s = SU.ReplaceInString(s, tokenNext, indexBeginTP, indexEndTP); } } static string fixFunction(string s, bool optionExplicit) { int indexBegin, indexEnd; bool ok = SU.GetBeginAndEndIndexes(s, "Function ", out indexBegin, out indexEnd); if (!ok) return s; string insertString = getReturnType(s); s = SU.ReplaceInString(s, insertString, indexBegin, indexEnd); s = fixFunctionEnding(s); if (optionExplicit) s = deleteAsAndSwap(s); s = Regex.Replace(s, @"\s+", " "); return s; } static string fixProperty(string s, bool optionExplicit) { int indexBegin, indexEnd; bool ok = SU.GetBeginAndEndIndexes(s, "Property ", out indexBegin, out indexEnd); if (!ok) return s; string insertString = getReturnType(s); s = SU.ReplaceInString(s, insertString, indexBegin, indexEnd); s = fixFunctionEnding(s); if (optionExplicit) s = deleteAsAndSwap(s); s = s.Replace("()", " "); return s; } static string fixSub(string s, bool optionExplicit) { int indexBegin, indexEnd; bool ok = SU.GetBeginAndEndIndexes(s, "Sub ", out indexBegin, out indexEnd); if (!ok) return s; s = SU.ReplaceInString(s, "void", indexBegin, indexEnd); if (optionExplicit) s = deleteAsAndSwap(s); s = s + " {"; s = Regex.Replace(s, @"\s+", " "); return s; } static string fixNamespace(string s) { int indexBegin = s.IndexOf("Namespace "); if (indexBegin >= 0) s = s + " {"; return s; } static string fixClass(string s) { int indexBegin = s.IndexOf("Class "); if (indexBegin >= 0) s = s + " {"; return s; } static public void DoSpecialOperations(string[] stringArray, bool optionExplicit) { for (int j = 0; j < stringArray.Length; j++) { stringArray[j] = fixFunction(stringArray[j], optionExplicit); stringArray[j] = fixProperty(stringArray[j], optionExplicit); stringArray[j] = fixSub(stringArray[j], optionExplicit); if (optionExplicit) stringArray[j] = deleteAsAndSwap(stringArray[j]); stringArray[j] = fixNew(stringArray[j]); stringArray[j] = fixNamespace(stringArray[j]); stringArray[j] = fixClass(stringArray[j]); } } static string fixSemiColon(string s, ReplaceTable rt) { string st = s.Trim(); if (st.Length == 0) return s; for (int k = rt.LineNumberNoSemicolonsLineStarts+1; k < rt.LineNumberNoSemicolonsLineEnds; k++) { if (st.StartsWith(rt.TheFileAsRows[k]) ) { if (st.IndexOf(" As ") >= 0) { if (!(st.IndexOf("Function ") >= 0 || st.IndexOf("Property ") >= 0 || st.IndexOf("Sub ") >= 0 ) ) { s = s + ";"; return s; } } return s; } } for (int k = rt.LineNumberNoSemicolonsLineEnds+1; k < rt.LineNumberStartReplace; k++) { if (st.EndsWith(rt.TheFileAsRows[k]) ) return s; } s = s + ";"; return s; } static public void AppendSemiColonUndSoWeiter(string[] stringArray, ReplaceTable rt) { for (int j = 0; j < stringArray.Length; j++) { stringArray[j] = fixSemiColon(stringArray[j], rt); } } static string fixNew(string s) { int indexBegin = s.IndexOf(" = new "); if (indexBegin < 1) return s; int indexEnd = indexBegin+7; int index = indexEnd; while (index >= 0 && index < s.Length && "(;".IndexOf(s[index++]) < 0); // index < s.Length and ";" is for VB6 int indexTypeEnd = index-1; string theType = s.Substring(indexEnd, indexTypeEnd-indexEnd); index = 0; while (index >= 0 && "\t ".IndexOf(s[index++]) >= 0); index = index-1; if (s.IndexOf("Dim") >= 0) s = s.Insert(index, theType + " "); return s; } static string fixPropertySet(string s) { int indexBegin = s.IndexOf("set {("); if (indexBegin < 1) return s; int indexEnd = indexBegin+5; s = s.Substring(0, indexEnd); return s; } static string fixComment(string s) { s = s.Replace("'", "//"); return s; } static public void FixTheRest(string[] stringArray) { for (int j = 0; j < stringArray.Length; j++) { stringArray[j] = fixPropertySet(stringArray[j]); stringArray[j] = fixComment(stringArray[j]); } } private VBNET() { } } }