Exemplo n.º 1
0
        private string PostParse(string vbSrc, PreParseResult preResult, string fixNamespaces)
        {
            string[] TmpLines;
            string   TmpLine;
            bool     TmpB = false;
            Int32    TmpStart;
            Int32    TmpI;
            string   TmpS;
            string   TmpS2;
            string   TmpS3;
            Int32    TmpBorderClassIndent = -1;
            bool     TmpInOperator        = false;
            bool     TmpInSelect          = false;
            string   RetS;

            //Replace TAB's with 2 spaces
            vbSrc = vbSrc.Replace(Chr(9), "  ");
            //repair string-append syntax 'str += "blabla"' --> 'str += "blabla"'
            vbSrc = vbSrc.Replace(" += \"", " += \"");
            //get all lines of code
            if (vbSrc.IndexOf(vbCrLf) > -1)
            {
                //Linebreak by CR-LF
                TmpLines = Split(vbSrc, vbCrLf);
            }
            else
            {
                //Linebreak by LF
                TmpLines = Split(vbSrc, vbLf);
            }
            //process each line (fix parser bugs)
            for (Int32 Y = 0; Y <= TmpLines.Length - 1; Y++)
            {
                if (ProviderHasParserFlag(ProviderParserFlags.HandleICSharpCodeParserBugs))
                {
                    //repair Parser-Bug "wrong enumumeration declarations" (remove incorrect 'private' prefix)
                    if (TmpB)
                    {
                        if (TmpLines(Y).ToLower.IndexOf("end enum") > -1)
                        {
                            TmpB = false;
                        }
                        else
                        {
                            TmpLines(Y) = TmpLines(Y).Replace("Private ", "");
                        }
                    }
                    else
                    {
                        if (KeywordInLine(TmpLines(Y), "enum ") > -1)
                        {
                            TmpB = true;
                        }
                    }
                    //repair Parser-Bug "wrong indexvariable incrementation in 'while' and 'for' statements"
                    //System.Math.Min(System.Threading.Interlocked.Increment(I),I-1) -->  I += 1
                    TmpStart = TmpLines(Y).ToLower.IndexOf("system.math.min(system.threading.interlocked.increment(");
                    if (TmpStart > -1)
                    {
                        TmpS = TmpLines(Y).Substring(TmpStart + 55);
                        TmpI = TmpS.IndexOf(")");
                        if (TmpI > -1)
                        {
                            //store code after statement in TmpS2
                            if (TmpS.Length > (5 + (TmpI * 2)))
                            {
                                TmpS2 = TmpS.Substring((5 + (TmpI * 2)), TmpS.Length - (5 + (TmpI * 2)));
                            }
                            else
                            {
                                TmpS2 = "";
                            }
                            //store I-variable in TmpS
                            TmpS = TmpS.Substring(0, TmpI);
                            //overwrite current line
                            TmpLines(Y) = TmpLines(Y).Substring(0, TmpStart) + TmpS + " += 1" + TmpS2;
                        }
                    }
                    //repair Parser-Bug "wrong explicit typecast"
                    //CType(ConversionHelpers.AsWorkaround(myvalue, GetType(mytype)), mytype) --> CType(myvalue, mytype)
                    TmpStart = TmpLines(Y).ToLower.IndexOf("ctype(conversionhelpers.asworkaround(");
                    if (TmpStart > -1)
                    {
                        TmpS = TmpLines(Y).Substring(TmpStart + 37);
                        TmpI = TmpS.IndexOf(",");
                        if (TmpI > -1)
                        {
                            //store value in TmpS2
                            TmpS2 = TmpS.Substring(0, TmpI);
                            TmpI  = TmpS.IndexOf("(");
                            if (TmpI > -1)
                            {
                                TmpS = TmpS.Substring(TmpI, TmpS.Length - TmpI);
                                TmpI = TmpS.IndexOf(")");
                                if (TmpI > -1)
                                {
                                    //store code after statement in TmpS3
                                    if (TmpS.Length > (5 + (TmpI * 2)))
                                    {
                                        TmpS3 = TmpS.Substring((5 + (TmpI * 2)), TmpS.Length - (5 + (TmpI * 2)));
                                    }
                                    else
                                    {
                                        TmpS3 = "";
                                    }
                                    //store type in TmpS
                                    TmpS = TmpS.Substring(0, TmpI);
                                    //overwrite current line
                                    TmpLines(Y) = TmpLines(Y).Substring(0, TmpStart) + "CType(" + TmpS2 + ", " + TmpS + ")" + TmpS3;
                                }
                            }
                        }
                    }
                    //repair Parser-Bug "multiple attributes on multiple lines have only one < on first and one > on last line"
                    //<MyAttrX()> _[CRLF]<MyAttrY()> _[CRLF]<MyAttrZ()>  -->  <MyAttrX(), _[CRLF] MyAttrY(), _[CRLF] MyAttrZ()>
                    if ((TmpLines(Y).Length > 3) && (TmpLines(Y).Substring(TmpLines(Y).Length - 3, 3) == "> _") && (TmpLines.Length > Y + 1))
                    {
                        //store next line without leading SPACE and TAB in TmpLine
                        TmpLine = StrTrimmLeft(StrTrimmLeft(TmpLines(Y + 1), Chr(9).ToString), " ");
                        if ((TmpLine.Length > 1) && (TmpLine.Substring(0, 1) == "<"))
                        {
                            //preserve Spaces before TmpLine in TmpS
                            if (TmpLines(Y + 1).Length > TmpLine.Length)
                            {
                                TmpS = TmpLines(Y + 1).Substring(0, TmpLines(Y + 1).Length - TmpLine.Length);
                            }
                            else
                            {
                                TmpS = "";
                            }
                            //replace "> _" with ", _" at end of this line
                            TmpLines(Y) = TmpLines(Y).Substring(0, TmpLines(Y).Length - 3) + ", _";
                            //replace "<" with " " at begin of next line
                            TmpLines(Y + 1) = TmpS + " " + TmpLine.Substring(1, TmpLine.Length - 1);
                        }
                    }
                }
            }
            //Lines -> String (Add Prefix: CRLF)
            vbSrc = vbCrLf + Join(TmpLines, vbCrLf);
            //fix Namespaces (remove)
            if (fixNamespaces != "")
            {
                foreach (string ns in Split(fixNamespaces, ","))
                {
                    vbSrc = vbSrc.Replace(vbCrLf + "Imports " + ns + vbCrLf, vbCrLf);
                }
            }
            //get all lines of code: String -> Lines (Remove Prefix: CRLF)
            TmpLines = Split(vbSrc.Substring(2, vbSrc.Length - 2), vbCrLf);
            //process each line (restore comments and regions)
            for (Int32 I = 0; I <= TmpLines.Length - 1; I++)
            {
                if ((TmpBorderClassIndent > -1) && (TmpLines(I).Length >= TmpBorderClassIndent + 9) && (TmpLines(I).Substring(TmpBorderClassIndent, 9) == "End Class"))
                {
                    //remove end of BorderClass
                    TmpBorderClassIndent = -1;
                    //mark line with KILL if its not last line
                    if ((TmpLines.Length > I + 1))
                    {
                        TmpLines(I) = "KILL";
                    }
                }
                else if (TmpLines(I).IndexOf("Class BorderClass") > -1)
                {
                    //remove begin of BorderClass
                    TmpLine = StrTrimmLeft(TmpLines(I), " ", true);
                    TmpBorderClassIndent = TmpLines(I).Length - TmpLine.Length;
                    TmpLines(I)          = "KILL";
                    if ((I > 0) && (TmpLines(I - 1) == ""))
                    {
                        //remove empty line before BorderClass (mark with KILL)
                        TmpLines(I - 1) = "KILL";
                    }
                    //mark next line after BorderClass with KILL if its empty and not last line
                    if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                    {
                        TmpLines(I + 1) = "KILL";
                    }
                }
                else
                {
                    if ((TmpBorderClassIndent > -1) && (TmpLines(I).Length > 2) && (StrTrimmLeft(TmpLines(I).Substring(0, 2), " ") == ""))
                    {
                        //correct indention (remove 2 spaces because of BorderClass)
                        TmpLines(I) = TmpLines(I).Substring(2, TmpLines(I).Length - 2);
                    }
                    //get line without leading spaces
                    TmpLine = StrTrimmLeft(TmpLines(I), " ", true);
                    //preserve spaces
                    TmpS = TmpLines(I).Substring(0, TmpLines(I).Length - TmpLine.Length);
                    if (TmpLine.Length > 0)
                    {
                        //skip empty lines
                        if ((TmpLine.Length > 25) && (TmpLine.Substring(0, 25).ToLower == "dim commentvar as string "))
                        {
                            //convert comment
                            TmpLines(I) = TmpS + CSharpToVbComment(TmpLine.Substring(28, TmpLine.Length - 29));
                            //mark next line with KILL if its empty and not last line
                            if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                            {
                                TmpLines(I + 1) = "KILL";
                            }
                        }
                        else if ((TmpLine.Length > 29) && (TmpLine.Substring(0, 29).ToLower == "private commentvar as string "))
                        {
                            //convert comment
                            TmpLines(I) = TmpS + CSharpToVbComment(TmpLine.Substring(32, TmpLine.Length - 33));
                            //mark next line with KILL if its empty and not last line
                            if ((TmpLines.Length > I + 1) && (TmpLines(I + 1) == ""))
                            {
                                TmpLines(I + 1) = "KILL";
                            }
                        }
                        else if ((TmpLine.Length > 24) && (TmpLine.Substring(0, 24).ToLower == "dim regionvar as string "))
                        {
                            //convert region or compilerarg
                            TmpLines(I) = CSharpToVbComment(TmpLine.Substring(27, TmpLine.Length - 28));
                        }
                        else if ((TmpLine.Length > 28) && (TmpLine.Substring(0, 28).ToLower == "private regionvar as string "))
                        {
                            //convert region or compilerarg
                            TmpLines(I) = CSharpToVbComment(TmpLine.Substring(31, TmpLine.Length - 32));
                        }
                        else if ((TmpLine // ERROR: Unknown binary operator Like
                                  ))
                        {
                            //Empty Line
                            TmpLines(I) = "";
                        }
                        else if (TmpLine.IndexOf("") > -1)
                        {
                            //restore Partial Class (new .NET 2.0 feature)
                            TmpLines(I) = TmpLines(I).Replace("", "").Replace("Class ", "Partial Class ");
                            //'restore Partial Class (new .NET 2.0 feature)
                            //TmpLines(i) = TmpS & "Partial " & TmpLine.Replace("PartialClassConverted", "")
                        }
                        else if (TmpLine.IndexOf("Operator_") > -1)
                        {
                            //restore "Operator Overloading" Part1 (new .NET 2.0 feature for VB.NET)
                            for (Int32 iOp = 0; iOp <= FOverloadableOperators.Length - 1; iOp++)
                            {
                                TmpLines(I)   = TmpLines(I).Replace("Function Operator_" + iOp.ToString, "Operator " + FOverloadableOperators(iOp));
                                TmpInOperator = true;
                            }
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //begin of SELECT CASE
                            TmpInSelect = true;
                        }
                        else if ((TmpLine // ERROR: Unknown binary operator Like
                                  ) && TmpInSelect)
                        {
                            //inside SELECT CASE
                            //delete EXIT SELECT, if next statement is CASE
                            if (NextCodeLineHasNoneOfKeywords(TmpLines, I, new string[] { "Case" }))
                            {
                                TmpLines(I) = "KILL";
                            }
                        }
                        else if ((TmpLine == "End Select") && TmpInSelect)
                        {
                            //end of SELECT CASE
                            TmpInSelect = false;
                        }
                        else if ((TmpLine == "End Function") && TmpInOperator)
                        {
                            //restore "Operator Overloading" Part2 (new .NET 2.0 feature for VB.NET)
                            //End Function -> End Operator
                            TmpLines(I) = TmpS + "End Operator";
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //repair compiration of Nothing (must be "is" instead of "=")
                            TmpLines(I) = TmpS + TmpLine.Replace("= Nothing", "Is Nothing");
                        }
                        else if (TmpLine // ERROR: Unknown binary operator Like
                                 )
                        {
                            //repair compiration of Not-Nothing (must be "is" instead of "=")
                            TmpLines(I) = TmpS + TmpLine.Replace("<> Nothing", "Is Nothing").Replace("If ", "If Not ");
                        }
                    }
                }
            }
            //get first lines comments and regions
            TmpS = "";
            for (Int32 I = 0; I <= preResult.FirstLinesComments.Length - 1; I++)
            {
                if (preResult.FirstLinesComments(I) != "")
                {
                    TmpS += CSharpToVbComment(preResult.FirstLinesComments(I)) + vbCrLf;
                }
            }
            //insert first lines at beginning
            RetS = TmpS + Join(TmpLines, vbCrLf) + vbCrLf;
            //remove all lines that are marked with       RetS = RetS.Replace("KILL" + vbCrLf, "");
            return(RetS);
        }
Exemplo n.º 2
0
        private PreParseResult PreParse(string csSrc)
        {
            string[]         TmpLines;
            string           TmpLine;
            string           TmpFirstComments = "";
            bool             TmpBeforeCode;
            bool             TmpInClass;
            Int32            TmpUnsupportedRangeCloseLine = -1;
            CommentAreaKinds TmpInCommentArea             = CommentAreaKinds.Supported;
            Int32            TmpBorderCloseLine;
            Int32            TmpI;
            Int32            TmpI2;
            PreParseResult   RetRes = new PreParseResult();

            //store original source lines
            if (csSrc.IndexOf(vbCrLf) > -1)
            {
                //Linebreak by CR-LF
                RetRes.OriginalSourceLines = Split(csSrc, vbCrLf);
            }
            else
            {
                //Linebreak by LF
                RetRes.OriginalSourceLines = Split(csSrc, vbLf);
            }
            //Replace TAB's with 2 spaces
            csSrc = csSrc.Replace(Chr(9), "  ");
            if (ProviderHasParserFlag(ProviderParserFlags.HandleComments))
            {
                //convert multiline comment-ranges to singleline comments
                //begin search on first sign (TmpI is current search position)
                TmpI = 0;
                while ((TmpI > -1) && (csSrc.IndexOf("/*", TmpI) > -1))
                {
                    //loop through every comment-range
                    TmpI  = csSrc.IndexOf("/*", TmpI);
                    TmpI2 = csSrc.IndexOf("*/", TmpI + 2);
                    if (TmpI2 > -1)
                    {
                        //check if this range-start is not allready a commentline
                        if (!((TmpI > 0) && (csSrc.Substring(TmpI - 1, 1) == "/")))
                        {
                            //TmpI is the start of the commentrange
                            if (csSrc.IndexOf(vbCrLf) > -1)
                            {
                                //Linebreak by CR-LF
                                TmpLines = Split(csSrc.Substring(TmpI, TmpI2 - TmpI + 2), vbCrLf);
                            }
                            else
                            {
                                //Linebreak by LF
                                TmpLines = Split(csSrc.Substring(TmpI, TmpI2 - TmpI + 2), vbLf);
                            }
                            //only modify comment-range if it has more than one line, otherwise skip
                            if (TmpLines.Length > 1)
                            {
                                //only one line
                                //  TmpLines(0) = "//" & TmpLines(0).Substring(2, TmpLines(0).Length - 4)
                                //Else
                                //multiple lines
                                for (Int32 L = 0; L <= TmpLines.Length - 1; L++)
                                {
                                    if (L == 0)
                                    {
                                        //first line
                                        TmpLines(L) = "//" + TmpLines(L).Substring(2, TmpLines(L).Length - 2);
                                    }
                                    else if (L == TmpLines.Length - 1)
                                    {
                                        //last line
                                        TmpLines(L) = "//" + TmpLines(L).Substring(0, TmpLines(L).Length - 2);
                                    }
                                    else
                                    {
                                        //middle lines
                                        TmpLines(L) = "//" + TmpLines(L);
                                    }
                                }
                                //store modified range
                                csSrc = csSrc.Substring(0, TmpI) + Join(TmpLines, vbCrLf) + csSrc.Substring(TmpI2 + 2, csSrc.Length - TmpI2 - 2);
                            }
                            else
                            {
                                //jump to next line
                                TmpI2 = csSrc.IndexOf(vbCrLf, TmpI);
                            }
                        }
                        else
                        {
                            //this range-start is a commentline, jump to next line
                            TmpI2 = csSrc.IndexOf(vbCrLf, TmpI);
                        }
                    }
                    //jump to TmpI2
                    TmpI = TmpI2;
                }
            }
            //get all lines of code
            if (csSrc.IndexOf(vbCrLf) > -1)
            {
                //Linebreak by CR-LF
                TmpLines = Split(csSrc + vbCrLf, vbCrLf);
            }
            else
            {
                //Linebreak by LF
                TmpLines = Split(csSrc + vbLf, vbLf);
            }
            //process each line
            TmpBeforeCode      = true;
            TmpBorderCloseLine = TmpLines.Length - 1;
            for (Int32 I = 0; I <= TmpLines.Length - 1; I++)
            {
                //get line without leading spaces
                TmpLine = StrTrimmLeft(StrTrimmLeft(TmpLines(I), " ", true), Chr(9).ToString, true);
                if (TmpLine.Length == 0)
                {
                    //empty line
                    if (TmpInClass && (TmpInCommentArea == CommentAreaKinds.Supported) && ProviderHasParserFlag(ProviderParserFlags.HandleComments))
                    {
                        TmpLines(I) = "string EmptyLineVar;";
                    }
                }
                else
                {
                    if ((TmpLine.Length > 1) && (TmpLine.Substring(0, 2) == "//"))
                    {
                        if (ProviderHasParserFlag(ProviderParserFlags.HandleComments))
                        {
                            //current line is a comment
                            if (TmpBeforeCode)
                            {
                                TmpFirstComments += TmpLine + vbCrLf;
                            }
                            else if (TmpInClass && (TmpUnsupportedRangeCloseLine == -1))
                            {
                                if ((TmpInCommentArea == CommentAreaKinds.Supported) && NextCodeLineHasNoneOfKeywords(TmpLines, I, new string[] {
                                    "get",
                                    "set"
                                }))
                                {
                                    TmpLines(I) = "string CommentVar = \"" + TmpLine.Replace("\"", "§§").Replace("\\", "§$") + "\";";
                                }
                            }
                        }
                    }
                    else if (TmpLine.Substring(0, 1) == "#")
                    {
                        if (ProviderHasParserFlag(ProviderParserFlags.HandleComments))
                        {
                            //current line is a compilerarg or region
                            if (TmpBeforeCode)
                            {
                                TmpFirstComments += TmpLine + vbCrLf;
                            }
                            else if (TmpInClass && (TmpInCommentArea == CommentAreaKinds.Supported))
                            {
                                TmpLines(I) = "string RegionVar = \"" + TmpLine.Replace("\"", "§§").Replace("\\", "§$") + "\";";
                            }
                        }
                    }
                    else
                    {
                        TmpBeforeCode = false;
                        if (((TmpLine.Length > 6) && (TmpLine.Substring(0, 6).ToLower == "using ")))
                        {
                            //before add border Class (USING statement)
                        }
                        else if ((TmpLine.ToLower.IndexOf("assembly: ") > -1))
                        {
                            //before add border Class (ASSEMBLY statement)
                        }
                        else if (((TmpLine.Length > 10) && (TmpLine.Substring(0, 10).ToLower == "Namespace ")))
                        {
                            if (TmpLine.IndexOf("{") > -1)
                            {
                                TmpI = GetCloseTagLine(TmpLines, I);
                            }
                            else
                            {
                                TmpI = GetCloseTagLine(TmpLines, I + 1);
                            }
                            if (TmpI > -1)
                            {
                                TmpBorderCloseLine = TmpI;
                            }
                            if ((I < TmpBorderCloseLine))
                            {
                                if (TmpLine.IndexOf("{") > -1)
                                {
                                    //add borderClass now after current line
                                    TmpI = I;
                                }
                                else
                                {
                                    //add borderClass now after next line witch should be '{'
                                    TmpI = I + 1;
                                }
                                TmpInClass     = true;
                                TmpLines(TmpI) = TmpLines(TmpI) + vbCrLf + "Class BorderClass" + vbCrLf + "{";
                                TmpLines(TmpBorderCloseLine) = "}" + vbCrLf + TmpLines(TmpBorderCloseLine);
                                RetRes.LineShifts.Add(RetRes.GetShift(TmpI) + TmpI);
                                RetRes.LineShifts.Add(RetRes.GetShift(TmpI) + TmpI + 1);
                                RetRes.LineShifts.Add(RetRes.GetShift(TmpBorderCloseLine) + TmpBorderCloseLine);
                            }
                        }
                        else
                        {
                            if ((I >= TmpBorderCloseLine))
                            {
                                TmpInClass = false;
                            }
                            else
                            {
                                if (!TmpInClass)
                                {
                                    for (Int32 J = I + 1; J <= TmpLines.Length - 1; J++)
                                    {
                                        if ((TmpLines(J).ToLower.IndexOf("Namespace") > -1))
                                        {
                                            TmpBorderCloseLine = J - 1;
                                            break; // TODO: might not be correct. Was : Exit For
                                        }
                                    }
                                    //add borderClass now before current line
                                    TmpInClass  = true;
                                    TmpLine     = "Class BorderClass" + vbCrLf + "{" + vbCrLf + TmpLines(I);
                                    TmpLines(I) = TmpLine;
                                    TmpLines(TmpBorderCloseLine) = TmpLines(TmpBorderCloseLine) + vbCrLf + "}";
                                    RetRes.LineShifts.Add(RetRes.GetShift(I) + I);
                                    RetRes.LineShifts.Add(RetRes.GetShift(I) + I + 1);
                                    RetRes.LineShifts.Add(RetRes.GetShift(TmpBorderCloseLine) + TmpBorderCloseLine);
                                }
                            }
                            if (ProviderHasParserFlag(ProviderParserFlags.HandleSomeNet2LangFeatures))
                            {
                                if (KeywordInLine(TmpLine, "Partial ") > -1)
                                {
                                    //support for .NET 2.0 Language keywords: "Partial" classes
                                    TmpLines(I) = TmpLine.Replace("Partial ", "").TrimEnd(' ') + "";
                                }
                                if (KeywordInLine(TmpLine, "operator ") > -1)
                                {
                                    //support for VB.NET 2.0 Language keywords: "Operator overloading" (C# supports operator overloading since v1.0)
                                    for (Int32 iOp = 0; iOp <= FOverloadableOperators.Length - 1; iOp++)
                                    {
                                        TmpLines(I) = TmpLine.Replace("operator " + FOverloadableOperators(iOp), "Operator_" + iOp.ToString);
                                    }
                                }
                            }
                        }
                        if (TmpUnsupportedRangeCloseLine == I)
                        {
                            TmpUnsupportedRangeCloseLine = -1;
                            TmpInCommentArea             = CommentAreaKinds.Supported;
                        }
                        else if ((KeywordInLine(TmpLine, "interface ") > -1))
                        {
                            if (TmpLine.LastIndexOf("{") > KeywordInLine(TmpLine, "interface "))
                            {
                                TmpI = I;
                            }
                            else
                            {
                                TmpI = I + 1;
                            }
                            TmpUnsupportedRangeCloseLine = GetCloseTagLine(TmpLines, TmpI);
                            TmpInCommentArea             = CommentAreaKinds.Unsupported;
                        }
                        else if ((KeywordInLine(TmpLine, "enum ") > -1))
                        {
                            if (TmpLine.LastIndexOf("{") > KeywordInLine(TmpLine, "enum "))
                            {
                                TmpI = I;
                            }
                            else
                            {
                                TmpI = I + 1;
                            }
                            TmpUnsupportedRangeCloseLine = GetCloseTagLine(TmpLines, TmpI);
                            TmpInCommentArea             = CommentAreaKinds.Unsupported;
                        }
                    }
                }
            }
            //assign first comments
            RetRes.FirstLinesComments = Split(TmpFirstComments, vbCrLf);
            //assign result string
            RetRes.ResultString = Join(TmpLines, vbCrLf);
            return(RetRes);
        }