Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete/Remove All Comments
#1
I tend to have a number of smaller well commented codes which eventually I roll together into a finished larger program. These smaller code sections can have quite a few lines of code and comments which get copied and pasted into the larger program. To remove the comments from the finished larger program I have been hunting and highlighting the comments and deleting them as I find them. I was thinking the IDE may be more helpful in this regard if there was an option under the EDIT to Remove ALL COMMENTS.
Reply
#2
You can probably use some sort of regex script to do this.
Tread on those who tread on you

Reply
#3
I know what they are but that's the sum total of my regex script knowledge. I would assume it could be very complicated to find every incident of a Rem or comment line and to distinguish the " ' " at the beginning of the line as opposed to a " ' " as part of the comment itself 
(ie REM: It's a key Do Loop v's the previous Do Loops)
(  '      :  It's a key Do Loop v's the previous Do Loops)

Looking at that example I suppose I could change my coding habit with comments to be sure I used REM instead of " ' " to simplify the regex script. Boy always something new to learn, never a dull moment when you're into coding.
Thanks Spriggsy
Reply
#4
Wouldn't this be as simple as writing a program that

1) read each line one line at a time
2) parse that line from left to right...
2a) Look for quote symbol so you can skip until next quote symbol in case something is like PRINT "Don't each cheese!"
2b) If you're not inside a quite, find the first comment symbol.  At that point, you can elimiate it and everything to the right.
3) Write the edited line to a new file called "No Comment " + name$
4) Loop until done
Reply
#5
@Dimster

In it's simplest form...

Code: (Select All)
Width 140, 42
_ScreenMove 0, 0
a$ = "i=0: Print " + Chr$(34) + "Don't!" + Chr$(34) + " ' Remove this " + Chr$(34) + " " + Chr$(34) + " ha!" + Chr$(13) + Chr$(10) + "Rem This " + Chr$(34) + "needs" + Chr$(34) + " to go, too!" + Chr$(13) + Chr$(10) + "Print " + Chr$(34) + "Aint that cool?" + Chr$(34)
Print a$

seed = 1
c = 0
Do
    q = InStr(seed, LCase$(a$), "rem ")
    If q Then
        a$ = Mid$(a$, 1, q - 1) + "'" + Mid$(a$, q + 3) ' Convert Rem to '
    Else
        Exit Do
    End If
    c = c + 1: If c > Len(a$) Then Print "Error": End ' Prevents some fail that caused an endless loop.
    seed = seed + 5
Loop

For i = 1 To Len(a$)
    x$ = Mid$(a$, i, 1)
    Select Case x$
        Case "'"
            If quote = 0 Then
                Remove = 1 - Remove
            End If
        Case Chr$(34)
            If Remove = 0 Then
                quote = 1 - quote
            End If
        Case Chr$(13), Chr$(10)
            Remove = 0: quote = 0
    End Select
    If Remove = 0 Then new$ = new$ + x$
    Rem Line below can be used to see how it progresses.
    '    Print "Remove ="; Remove, "quote ="; quote, "x$ = "; x$, "new$ = "; new$ : Sleep
Next
Print new$

Applied to remove lines that are completely remarked and to get rid of trailing colons...

Code: (Select All)
Width 80, 37
_Font 16
_ScreenMove _Middle
Dim i As _Integer64
Dim col As Integer

For demo = 1 To 2
    Color 3, 0: Print "DEMO"; demo; "WITH COMMENTS...": Print: Color 7, 0
    If demo = 1 Then
        a$ = "i=0: Print " + Chr$(34) + "Don't!" + Chr$(34) + " ' Remove this " + Chr$(34) + " " + Chr$(34) + " ha!" + Chr$(13) + Chr$(10) + "j = 0: Rem This " + Chr$(34) + "needs" + Chr$(34) + " to go, too!" + Chr$(13) + Chr$(10) + "Print " + Chr$(34) + "Aint that cool?" + Chr$(34)
    Else
        a$ = "i=0: Print " + Chr$(34) + "Don't!" + Chr$(34) + " ' Remove this " + Chr$(34) + " " + Chr$(34) + " ha!" + Chr$(13) + Chr$(10) + "Rem This " + Chr$(34) + "needs" + Chr$(34) + " to go, too!" + Chr$(13) + Chr$(10) + "Print " + Chr$(34) + "Aint that cool?" + Chr$(34)
    End If

    For w = 1 To Len(a$) ' This is just a little 1/2 ASCII routine to change the font color for remarks.
        x$ = Mid$(a$, w, 1)
        Select Case x$
            Case "'", "R" ' R only appears as Rem in this play code, that's why we can get away with it in this demo.
                If quote = 0 Then
                    Color 14, 0
                End If
            Case Chr$(34)
                If Remove = 0 Then
                    quote = 1 - quote
                End If
            Case Chr$(13), Chr$(10)
                Color 7, 0
        End Select

        If Mid$(a$, w, 1) = Chr$(13) Then
            Print
        ElseIf Mid$(a$, w, 1) = Chr$(10) Then
            Print
        Else
            Print Mid$(a$, w, 1);
        End If
    Next
    Color 7, 0
    seed = 1
    c = 0
    Do
        q = InStr(seed, LCase$(a$), "rem ")
        If q Then
            a$ = Mid$(a$, 1, q - 1) + "'" + Mid$(a$, q + 3) ' Convert Rem to '
        Else
            Exit Do
        End If
        c = c + 1: If c > Len(a$) Then Print "Error": End ' Prevents some fail that caused an endless loop.
        seed = seed + 5
    Loop
    Print: Print
    Remove = 0: quote = 0: col = 0: i = 0: new$ = ""
    Do
        i = i + 1: col = col + 1
        x$ = Mid$(a$, i, 1)
        Select Case x$
            Case "'"
                If quote = 0 Then
                 If quote = 0 Then
                    q = InStr(i, a$ + Chr$(13), Chr$(13))
                    If col = 1 Then
                        a$ = Mid$(a$, 1, i - 1) + Mid$(a$, q + 2)
                        i = i - 1: col = 0
                    Else
                        a$ = Mid$(a$, 1, i - 1) + Mid$(a$, q)
                        i = i - 1: col = 0
                    End If
                    If Mid$(a$, i - 2, 2) = ": " Then
                        new$ = Mid$(new$, 1, Len(new$) - 2) ' Remove trailing colon.
                    End If
                    _Continue
                End If
                End If
            Case Chr$(34)
                quote = 1 - quote
            Case Chr$(13), Chr$(10)
                quote = 0: col = 0
        End Select
        new$ = new$ + x$
    Loop Until i >= Len(a$)
    Color 3, 0: Print: Print "WITHOUT COMMENTS": Print: Color 7, 0
    Print new$: Print
    Print "-----------------------------------------------"
Next

I don't know if QB64 still uses that _ symbol to wrap a code line. If it does, that would have to be included in an analysis.

Play around with it, and let me know if there are any chinks in the armor.

Pete
Reply
#6
Couldn't the Find/Replace All function be adapted to include Replace After (or something similar) so a search for ' or Rem woudl remove all code after them in the line?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#7
Back in the day it was called Remline, but it was always an independent app and never added to QBasic.

Pete
Shoot first and shoot people who ask questions, later.
Reply




Users browsing this thread: 1 Guest(s)