Beispiel #1
0
        public static string StripComments(string mdx)
        {
            MDXSplitStatus status            = MDXSplitStatus.InMDX;
            StringBuilder  ret               = new StringBuilder();
            int            iPos              = 0;
            int            blockCommentCount = 0;

            while (iPos < mdx.Length)
            {
                try
                {
                    if (status == MDXSplitStatus.InMDX)
                    {
                        if (mdx.SafeSubstring(iPos, 2) == "/*")
                        {
                            status            = MDXSplitStatus.InBlockComment;
                            blockCommentCount = 1;
                            iPos += 1;
                        }
                        else if (mdx.SafeSubstring(iPos, 2) == "--")
                        {
                            status = MDXSplitStatus.InDashComment;
                            iPos  += 1;
                        }
                        else if (mdx.SafeSubstring(iPos, 2) == "//")
                        {
                            status = MDXSplitStatus.InSlashComment;
                            iPos  += 1;
                        }
                        else
                        {
                            ret.Append(mdx[iPos]);
                        }
                    }
                    else if (status == MDXSplitStatus.InDashComment || status == MDXSplitStatus.InSlashComment)
                    {
                        if (mdx.SafeSubstring(iPos, Environment.NewLine.Length) == Environment.NewLine)
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InBlockComment)
                    {
                        // nested InBlockComments are possible so we need to count how often we have entered an InBlockComment
                        if (mdx.SafeSubstring(iPos, 2) == "/*")
                        {
                            blockCommentCount += 1;
                            iPos += 1;
                        }
                        if (mdx.SafeSubstring(iPos, 2) == "*/")
                        {
                            // check if its the last InBlockComment-Block open
                            if (blockCommentCount == 1)
                            {
                                status            = MDXSplitStatus.InMDX;
                                blockCommentCount = 0;
                                iPos += 1;
                            }
                            else // close the last InBlockComment-Block
                            {
                                blockCommentCount -= 1;
                                iPos += 1;
                            }
                        }
                    }
                    iPos++;
                }
                catch (Exception ex)
                {
                }
            }
            return(ret.ToString().Trim(Environment.NewLine.ToCharArray()));
        }
        private static string[] MDXStatementSplit(string sMDX, bool bRemoveComments)
        {
            MDXSplitStatus status        = MDXSplitStatus.InMDX;
            int            iPos          = 0;
            int            iLastSplit    = 0;
            int            iCommentStart = 0;

            System.Collections.Generic.List <string> arrSplits = new System.Collections.Generic.List <string>();

            while (iPos < sMDX.Length)
            {
                try
                {
                    if (status == MDXSplitStatus.InMDX)
                    {
                        if (sMDX.Substring(iPos, 2) == "/*")
                        {
                            status        = MDXSplitStatus.InBlockComment;
                            iCommentStart = iPos;
                            iPos         += 1;
                        }
                        else if (sMDX.Substring(iPos, 2) == "--")
                        {
                            status        = MDXSplitStatus.InDashComment;
                            iCommentStart = iPos;
                            iPos         += 1;
                        }
                        else if (sMDX.Substring(iPos, 2) == "//")
                        {
                            status        = MDXSplitStatus.InSlashComment;
                            iCommentStart = iPos;
                            iPos         += 1;
                        }
                        else if (sMDX.Substring(iPos, 1) == "[")
                        {
                            status = MDXSplitStatus.InBrackets;
                        }
                        else if (sMDX.Substring(iPos, 1) == "\"")
                        {
                            status = MDXSplitStatus.InDoubleQuotes;
                        }
                        else if (sMDX.Substring(iPos, 1) == "'")
                        {
                            status = MDXSplitStatus.InSingleQuotes;
                        }
                        else if (sMDX.Substring(iPos, 1) == ";") //split on semicolon only when it's in general MDX context
                        {
                            arrSplits.Add(TrimBlankLines(sMDX.Substring(iLastSplit, iPos - iLastSplit)) + ";");
                            iLastSplit = iPos + 1;
                        }
                    }
                    else if (status == MDXSplitStatus.InDashComment || status == MDXSplitStatus.InSlashComment)
                    {
                        if (Environment.NewLine.Contains(sMDX.Substring(iPos, 1)))
                        {
                            status = MDXSplitStatus.InMDX;
                            if (bRemoveComments)
                            {
                                sMDX  = sMDX.Remove(iCommentStart, iPos - iCommentStart);
                                iPos -= (iPos - iCommentStart);
                            }
                        }
                    }
                    else if (status == MDXSplitStatus.InBlockComment)
                    {
                        if (sMDX.Substring(iPos, 2) == "*/")
                        {
                            status = MDXSplitStatus.InMDX;
                            iPos  += 1;
                            if (bRemoveComments)
                            {
                                sMDX  = sMDX.Remove(iCommentStart, iPos - iCommentStart);
                                iPos -= (iPos - iCommentStart);
                            }
                        }
                    }
                    else if (status == MDXSplitStatus.InBrackets)
                    {
                        if (sMDX.Substring(iPos, 1) == "]" && sMDX.Substring(iPos, 2) == "]]")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.Substring(iPos, 1) == "]" && sMDX.Substring(iPos, 2) != "]]")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InDoubleQuotes)
                    {
                        if (sMDX.Substring(iPos, 1) == "\"" && sMDX.Substring(iPos, 2) == "\"\"")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.Substring(iPos, 1) == "\"" && sMDX.Substring(iPos, 2) != "\"\"")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InSingleQuotes)
                    {
                        if (sMDX.Substring(iPos, 1) == "'" && sMDX.Substring(iPos, 2) == "''")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.Substring(iPos, 1) == "'" && sMDX.Substring(iPos, 2) != "''")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    iPos++;
                }
                catch
                {
                    if (TrimBlankLines(sMDX.Substring(iLastSplit)).Length > 0)
                    {
                        arrSplits.Add(TrimBlankLines(sMDX.Substring(iLastSplit)));
                    }
                    iPos = sMDX.Length;
                }
            }
            return(arrSplits.ToArray());
        }
Beispiel #3
0
        public static SortedDictionary <int, MdxScriptCommand> GetCommandsFromText(string sMDX, int commandNr)
        {
            MDXSplitStatus          status = MDXSplitStatus.InMDX;
            MdxScriptCommand        newCmd;
            List <MdxScriptCommand> nestedScopes = new List <MdxScriptCommand>();
            int           iPos              = 0;
            int           iLastSplit        = 0;
            int           blockCommentCount = 0;
            List <string> arrSplits         = new List <string>();
            SortedDictionary <int, MdxScriptCommand> commands = new SortedDictionary <int, MdxScriptCommand>();

            while (iPos < sMDX.Length)
            {
                try
                {
                    if (status == MDXSplitStatus.InMDX)
                    {
                        if (sMDX.SafeSubstring(iPos, 2) == "/*")
                        {
                            status            = MDXSplitStatus.InBlockComment;
                            blockCommentCount = 1;
                            iPos += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 2) == "--")
                        {
                            status = MDXSplitStatus.InDashComment;
                            iPos  += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 2) == "//")
                        {
                            status = MDXSplitStatus.InSlashComment;
                            iPos  += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "[")
                        {
                            status = MDXSplitStatus.InBrackets;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "\"")
                        {
                            status = MDXSplitStatus.InDoubleQuotes;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "'")
                        {
                            status = MDXSplitStatus.InSingleQuotes;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == ";") //split on semicolon only when it's in general MDX context
                        {
                            newCmd = new MdxScriptCommand(sMDX.SafeSubstring(iLastSplit, iPos - iLastSplit) + ";", commandNr, nestedScopes.Count);

                            if (nestedScopes.Count > 0)
                            {
                                newCmd.ParentCommand = nestedScopes.Last();
                            }

                            if (newCmd.CommandType == MdxScriptCommandType.SCOPE)
                            {
                                nestedScopes.Add(newCmd);
                            }
                            else if (newCmd.CommandType == MdxScriptCommandType.END_SCOPE)
                            {
                                nestedScopes.Last().CorrespondingEndScopeCommand = newCmd;
                                nestedScopes.RemoveAt(nestedScopes.Count - 1);
                            }

                            commands.Add(newCmd.CommandNumber, newCmd);
                            commandNr++;

                            iLastSplit = iPos + 1;
                        }
                    }
                    else if (status == MDXSplitStatus.InDashComment || status == MDXSplitStatus.InSlashComment)
                    {
                        if (sMDX.SafeSubstring(iPos, Environment.NewLine.Length) == Environment.NewLine)
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InBlockComment)
                    {
                        // nested InBlockComments are possible so we need to count how often we have entered an InBlockComment
                        if (sMDX.SafeSubstring(iPos, 2) == "/*")
                        {
                            blockCommentCount += 1;
                            iPos += 1;
                        }
                        if (sMDX.SafeSubstring(iPos, 2) == "*/")
                        {
                            // check if its the last InBlockComment-Block open
                            if (blockCommentCount == 1)
                            {
                                status            = MDXSplitStatus.InMDX;
                                blockCommentCount = 0;
                                iPos += 1;
                            }
                            else // close the last InBlockComment-Block
                            {
                                blockCommentCount -= 1;
                                iPos += 1;
                            }
                        }
                    }
                    else if (status == MDXSplitStatus.InBrackets)
                    {
                        if (sMDX.SafeSubstring(iPos, 1) == "]" && sMDX.SafeSubstring(iPos, 2) == "]]")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "]" && sMDX.SafeSubstring(iPos, 2) != "]]")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InDoubleQuotes)
                    {
                        if (sMDX.SafeSubstring(iPos, 1) == "\"" && sMDX.SafeSubstring(iPos, 2) == "\"\"")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "\"" && sMDX.SafeSubstring(iPos, 2) != "\"\"")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                    else if (status == MDXSplitStatus.InSingleQuotes)
                    {
                        if (sMDX.SafeSubstring(iPos, 1) == "'" && sMDX.SafeSubstring(iPos, 2) == "''")
                        {
                            iPos += 1;
                        }
                        else if (sMDX.SafeSubstring(iPos, 1) == "'" && sMDX.SafeSubstring(iPos, 2) != "''")
                        {
                            status = MDXSplitStatus.InMDX;
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                iPos++;
            }
            return(commands);
        }