using System; using System.Collections.Specialized; using System.Text.RegularExpressions; using SU = Laj.StringUtils; namespace Laj.BabbelFisken { // Last modified: 2002-09-30 // Version: 0.50 ID:LAJ44006 // 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. // TODO: // Nested functions // Program file instead of unit file // Changes: // Version: 0.50 2002-09-30 // if (stringArray[j].IndexOf("//") != 0) added // Version: 0.43 // var => out instead of ref for Delphi functions/procedures. internal class Delphi { static string fixComment(string s) { s = s.Replace("{", "//"); s = s.Replace("}", ""); s = s.Replace("(*", "/*"); s = s.Replace("*)", "*/"); return s; } static public void FixCommentsUndSoWeiter(string[] stringArray) { for (int j = 0; j < stringArray.Length; j++) { stringArray[j] = fixComment(stringArray[j]); } } static string getFunctionOrProcedureName(string s) { s = s.TrimStart(); int index = s.IndexOf(" "); if (index < 0) return "GAIS"; s = s.Substring(index).TrimStart(); index = s.IndexOf("("); if (index < 0) index = s.IndexOf(":"); return (index < 0) ? s : s.Substring(0, index).TrimStart(); } static string getReturnType(string s) { int indexBegin = s.LastIndexOf(")"); if (indexBegin < 0) indexBegin = s.LastIndexOf(":"); if (indexBegin < 0) return ""; s = s.Substring(indexBegin+1); s = s.Replace(":", ""); s = s.Replace(";", ""); s = s.Trim(); return s; } static string fixFunctionEnding(string s) { int index = s.LastIndexOf(")"); if (index < 0) index = s.LastIndexOf(":"); s = index > 0 ? s.Substring(0, index+1) : s; return s; } static string deleteSemicolonAndSwap(string s) { string tokenPrevious, tokenNext; bool ok; int indexBeginTP, indexEndTP, indexBeginTN, indexEndTN; for(;;) //Repeat until no more ":" { int index = s.IndexOf(":"); string stopString = "(;"; ok = SU.GetPreviousToken(s, index-1, stopString, out tokenPrevious, out indexBeginTP, out indexEndTP); if (tokenPrevious.StartsWith("var ") ) { tokenPrevious = tokenPrevious.Substring(4); indexBeginTP += 4; } if (!ok) return s; stopString = ");"; ok = SU.GetNextToken(s, index+1, stopString, out tokenNext, out indexBeginTN, out indexEndTN); if (!ok) return s; s = SU.ReplaceInString(s, " " + tokenPrevious, indexBeginTN, indexEndTN); s = s.Remove(index, 1); s = SU.ReplaceInString(s, " " + tokenNext, indexBeginTP, indexEndTP); } } static StringCollection publicFunctions = new StringCollection(); static bool functionInterfaceSection = false; static string fixFunctionAndProcedure(string s) { if (s.IndexOf("interface") >= 0) { functionInterfaceSection = true; } if (s.IndexOf("implementation") >= 0) { functionInterfaceSection = false; } if (!(s.TrimStart().IndexOf("function") == 0 || s.TrimStart().IndexOf("procedure") == 0) ) { return s; } if (functionInterfaceSection) { string fn = getFunctionOrProcedureName(s); publicFunctions.Add(fn.ToLower(Utils.InvCI) ); s = "// GAIS " + s; } else { bool fp = false; string fn = getFunctionOrProcedureName(s); string sfp = ""; string insertString = ""; int indexBegin, indexEnd; if (s.TrimStart().IndexOf("function") >= 0) { fp = true; sfp = "function "; insertString = getReturnType(s); } else if (s.TrimStart().IndexOf("procedure") >= 0) { fp = true; sfp = "procedure "; insertString = "void"; } if (fp) { bool ok = SU.GetBeginAndEndIndexes(s, sfp, out indexBegin, out indexEnd); if (insertString.Trim().Length > 0) s = SU.ReplaceInString(s, insertString, indexBegin, indexEnd); s = fixFunctionEnding(s); s = deleteSemicolonAndSwap(s); s = s.Replace(";", ", "); s = Regex.Replace(s, @"\bvar\b", "out"); s = Regex.Replace(s, @"\s+", " "); if (publicFunctions.Contains(fn.ToLower(Utils.InvCI) ) ) { s = "static public " + s; } else { s = "static " + s; } } } return s; } static bool endOfSection(string s, string sectionName) { string testString; testString = "begin"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; testString = "const"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; testString = "var"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; testString = "type"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; testString = "function"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; testString = "procedure"; if (testString != sectionName && s.IndexOf(testString) >= 0) return true; return false; } static bool typeSection = false; static bool recordSection = false; static string fixType(string s) { const string sectionName = "type"; if (s.IndexOf(sectionName) >= 0) { typeSection = true; } if (endOfSection(s, sectionName) ) { typeSection = false; return s; } if (typeSection) { if (s.IndexOf(" record") >= 0) { recordSection = true; int index = s.IndexOf("="); if (index <= 0) return s; int indexEnd = s.Length-1; s = "struct " + s.Substring(0, index-1) + " {"; return s; } if (s.IndexOf("}") >= 0) { recordSection = false; return s; } if (recordSection) { s = deleteSemicolonAndSwap(s); } else { if ((s.Trim().Length > 0) && (s.TrimStart().IndexOf("//") != 0) ) s = "// GAIS " + s; } } return s; } static bool constSection = false; static string fixConst(string s) { const string sectionName = "const"; if (s.IndexOf(sectionName) >= 0) { constSection = true; s = Regex.Replace(s, @"\b" + sectionName + @"\b", ""); } if (endOfSection(s, sectionName) ) { constSection = false; return s; } if (constSection) { int index = s.IndexOf(":"); if (index > 0) return s; index = s.IndexOf("="); if (index < 0) return s; s = "const " + s; } return s; } static bool varSection = false; static string fixVar(string s) { const string sectionName = "var"; if (s.IndexOf(sectionName) >= 0) { varSection = true; s = Regex.Replace(s, @"\b" + sectionName + @"\b", ""); } if (endOfSection(s, sectionName) ) { varSection = false; return s; } if (varSection) { s = deleteSemicolonAndSwap(s); } return s; } static public void DoSpecialOperations(string[] stringArray) { publicFunctions.Clear(); for (int j = 0; j < stringArray.Length; j++) { if (stringArray[j].IndexOf("//") != 0) { stringArray[j] = fixType(stringArray[j]); stringArray[j] = fixFunctionAndProcedure(stringArray[j]); stringArray[j] = fixConst(stringArray[j]); stringArray[j] = fixVar(stringArray[j]); } } } private Delphi() { } } }