Exemplo n.º 1
0
        private string ConvertSource(string csSrc, PreParseResult preResult)
        {
            IRefactoryProvider TmpRefactoryProvider;
            Int32 TmpI;
            Int32 TmpLine;

            string[] TmpLines;
            string   TmpS;
            string   RetS = "";

            //get current RefacotryProvider
            TmpRefactoryProvider = SettingsManager.CurrentRefactoryProvider;
            //convert with the RefactoryProvider
            try {
                RetS = TmpRefactoryProvider.ConvertCStoVB(csSrc);
            } catch (NetVertException ex) {
                //parser error, modify text and re-throw exception
                TmpS     = ex.Message.Substring(8, ex.Message.Length - 9);
                TmpI     = TmpS.IndexOf(" ");
                TmpLine  = Int32.Parse(TmpS.Substring(0, TmpI));
                TmpLine -= preResult.GetShift(TmpLine);
                if (TmpLine == 0)
                {
                    TmpLine = 1;
                }
                TmpS     = "Line " + TmpLine.ToString + TmpS.Substring(TmpI, TmpS.Length - TmpI);
                TmpLines = preResult.OriginalSourceLines;
                if (TmpLine > 1)
                {
                    if (TmpLine > 2)
                    {
                        TmpS += vbCrLf + "==== " + TmpLines(TmpLine - 3);
                    }
                    TmpS += vbCrLf + "==== " + TmpLines(TmpLine - 2);
                }
                if (TmpLines.Length >= TmpLine)
                {
                    TmpS += vbCrLf + "==>> " + TmpLines(TmpLine - 1);
                    if (TmpLines.Length > TmpLine)
                    {
                        TmpS += vbCrLf + "==== " + TmpLines(TmpLine);
                        if (TmpLines.Length > (TmpLine + 1))
                        {
                            TmpS += vbCrLf + "==== " + TmpLines(TmpLine + 1);
                        }
                    }
                }
                throw new NetVertException(TmpS);
            }
            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);
        }