0

What are the rules that vb6 uses to find the apostrophe that marks the beginning of a commented out portion of a line(s)?

I don't feel confident about my ability to define such rules because of :

  • apostrophes inside string literals
  • apostrophes inside nested double quotes in string operations
  • the fact that double quotes can occur in comments
  • the fact that a line can be continued over multiple lines
  • colons breaking up a single line
  • possible other rules that I'm unaware of
kjack
  • 2,004
  • 3
  • 26
  • 41
  • 1
    See also the question [Is the VB6 language syntax defined online](http://stackoverflow.com/questions/1129149/visual-basic-6-0-language-syntax) – MarkJ Nov 01 '13 at 18:45

3 Answers3

2

Apostrophes in string literals are ignored. Apostrophes in comments also ignored (because they are already in a comment). You cannot put as apostrophe in a multi-line statement unless it is on the last line of the statement -- in which case it just follows the normal rules.

Why are you so concerned?

Rob
  • 3,315
  • 1
  • 24
  • 35
  • You sorta can put an apostrophe on a line that's not the last line of a multi-line statement, it will comment out the subsequent line(s). – kjack Nov 01 '13 at 19:12
  • I want to know because I am trying to eliminate dead code and unused subs functions, variables and reduce the scope on others and to identify that I want to ignore calls etc that are commented out – kjack Nov 01 '13 at 19:13
  • @kjack OMG just get [MZ Tools](http://www.mztools.com/v3/mztools3.aspx) - it's free - and use the code review tool. "MZ-Tools can review your source code at project-group, project or file level (through context menus) detecting unused variables, constants, parameters, private procedures, and so on." – MarkJ Nov 01 '13 at 20:16
  • @MarkJ mz tools is great but it won't identify methods and variables that are declared Public but only called in own file or will it? – kjack Nov 01 '13 at 21:09
2

If you really want to know, the VBA language specification is online on MSDN with a BNF grammar. VBA is 99% equivalent to VB6 - certainly the rules about identifying comments must be identical.


And if you are trying to eliminate dead code - just get MZ-Tools! It's free. Use the code review tool. "MZ-Tools can review your source code at project-group, project or file level (through context menus) detecting unused variables, constants, parameters, private procedures, and so on." To eliminate unused subs and functions, use the MZ-Tools feature that lists all callers.


EDIT There is discussion in the comments about how to eliminate items which have been unecessarily declared as Public: MZ Tools does not help with this.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • I use MZ tools all the time and couldn't do without it. But it has some limitations. I'm in a project where a ton of stuff in modules has been declared public, mostly unnecessarily. From the documentation of MZ tools : For performance reasons, the source code review checks only private declarations, that is, declarations that can be used only inside the module where they are declared. – kjack Nov 02 '13 at 08:02
  • 1
    @kjack: Make them private and let the VB6 compiler check if these are called. Then revert selectively to public which are needed. Finally use MZ-Tools to clean up the left over private ones. – wqw Nov 02 '13 at 20:02
  • +1 @wqw If you need to obsessively check a large number of items, I guess you could automate this? Automatically modify the code file making one item at a time private, and then [trigger the VB6 compiler from the command line](http://stackoverflow.com/questions/3444505/what-are-the-command-line-options-for-the-vb6-ide-compiler) to see whether the project still builds? – MarkJ Nov 03 '13 at 10:09
  • @wqw The thing the compiler just stops on the first error and there are umpteen methods and variables that I want to make sure have the least scope possible and delete unused ones. – kjack Nov 03 '13 at 15:05
  • 1
    @MarkJ You suggest automating part of it. I'm trying to do that. – kjack Nov 03 '13 at 15:07
  • @kjack Good luck, if you have time post back and let us know how you get on – MarkJ Nov 04 '13 at 16:36
0

This seems to work on any examples I tried The only rule it's using is to ignore apostrophes inside string literals and it doesn't account for the use of the word rem to start a comment. To my shame I never realised until yesterday that """" (4 double quotes) was a string literal containing a single double quote.

Public Function CommentStripOut(ByVal strLine As String) As String


Dim InLiteral As Boolean
Dim strReturn As String
Dim LenLine As Long

Dim counter As Long
Dim s1 As String
Dim s2 As String


strReturn = strLine
LenLine = Len(strLine)

InLiteral = False


For counter = 1 To LenLine

    s1 = Mid$(strLine, counter, 1)

    If counter < LenLine Then
        s2 = Mid$(strLine, counter + 1, 1)
    Else
        s2 = ""
    End If

    If s1 = """" Then
        If Not InLiteral Then
            InLiteral = True
        Else
            If s2 = """" Then
                counter = counter + 1
                'skip on by 1 because
                'want to treat escaped
                'double quote as a single
                'character
            Else
                InLiteral = False
            End If
        End If
    Else
        If Not InLiteral Then
            If s1 = "'" Then
                strReturn = Left$(strLine, counter - 1)
                Exit For
            End If
        End If
    End If


Next counter

CommentStripOut = strReturn

End Function
kjack
  • 2,004
  • 3
  • 26
  • 41