Task: Strip block comments








VB: 






Dim t As String 
Dim s() As Byte 
Dim j As Integer 
Dim SourceLength As Integer 
Dim flag As Boolean
Private Sub Block_Comment(sOpBC As String, sClBC As String)
    
    flag = False
    Do While j < SourceLength - 2
        Select Case s(j)
            Case Asc(Left(sOpBC, 1))
                If s(j + 1) = Asc(Right(sOpBC, 1)) Then
                    
                    j = j + 2
                    Block_Comment sOpBC, sClBC
                End If
            Case Asc(Left(sClBC, 1))
                If s(j + 1) = Asc(Right(sClBC, 1)) Then
                    
                    flag = True
                    j = j + 2
                    Exit Do
                End If
                
            Case Else
        End Select
        j = j + 1
    Loop
    If Not flag Then MsgBox "Error, missing close block comment delimeter"
End Sub
Private Sub String_Literal()
    
    flag = False
    Do While j < SourceLength - 2
        If s(j) = Asc("""") Then
            If s(j + 1) = Asc("""") Then
                
                t = t + Chr(s(j))
                j = j + 1
            Else
                
                flag = True
                t = t + Chr(s(j))
                j = j + 1
                Exit Do
            End If
        End If
        t = t + Chr(s(j))
        j = j + 1
    Loop
    If Not flag Then MsgBox "Error, missing closing string delimeter"
End Sub
Private Sub Other_Text(Optional sOpBC As String = "/*", Optional sClBC As String = "*/")
    If Len(sOpBC) <> 2 Then
        MsgBox "Error, open block comment delimeter must be 2" & _
        " characters long, got " & Len(sOpBC) & " characters"
        Exit Sub
    End If
    If Len(sClBC) <> 2 Then
        MsgBox "Error, close block comment delimeter must be 2" & _
        " characters long, got " & Len(sClBC) & " characters"
        Exit Sub
    End If
    Do While j < SourceLength - 1
        Select Case s(j)
            Case Asc(""""):
                t = t + Chr(s(j))
                j = j + 1
                String_Literal
            Case Asc(Left(sOpBC, 1))
                If s(j + 1) = Asc(Right(sOpBC, 1)) Then
                    
                    j = j + 2
                    Block_Comment sOpBC, sClBC
                Else
                    t = t + Chr(s(j))
                    j = j + 1
                End If
            Case Else
                t = t + Chr(s(j))
                j = j + 1
        End Select
    Loop
    If j = SourceLength - 1 Then t = t + Chr(s(j))
End Sub
Public Sub strip_block_comment()
    Dim n As String
    n = n & "/**" & vbCrLf
    n = n & "* Some comments /*NESTED COMMENT*/" & vbCrLf
    n = n & "* longer comments here that we can parse." & vbCrLf
    n = n & "*" & vbCrLf
    n = n & "* Rahoo" & vbCrLf
    n = n & "*/" & vbCrLf
    n = n & "mystring = ""This is the """"/*"""" open comment block mark.""" & vbCrLf
    
    
    
    n = n & "function subroutine() {" & vbCrLf
    n = n & "a = /* inline comment */ b + c ;" & vbCrLf
    n = n & "}" & vbCrLf
    n = n & "/*/ <-- tricky /*NESTED*/ comments */" & vbCrLf
    n = n & "" & vbCrLf
    n = n & "/**" & vbCrLf
    n = n & "* Another comment." & vbCrLf
    n = n & "*/" & vbCrLf
    n = n & "function something() {" & vbCrLf
    n = n & "}"
    s = StrConv(n, vbFromUnicode)
    j = 0
    t = ""
    SourceLength = Len(n)
    Other_Text 
    Debug.Print "Original text:"
    Debug.Print String$(60, "-")
    Debug.Print n & vbCrLf
    Debug.Print "Text after deleting comment blocks, preserving string literals:"
    Debug.Print String$(60, "-")
    Debug.Print t
End Sub




Ada: with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with Ada.Command_Line;

procedure Strip is
   use Ada.Strings.Unbounded;
   procedure Print_Usage is
   begin
      Ada.Text_IO.Put_Line ("Usage:");
      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put_Line ("   strip <file> [<opening> [<closing>]]");
      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put_Line ("      file: file to strip");
      Ada.Text_IO.Put_Line ("      opening: string for opening comment");
      Ada.Text_IO.Put_Line ("      closing: string for closing comment");
      Ada.Text_IO.New_Line;
   end Print_Usage;

   Opening_Pattern : Unbounded_String := To_Unbounded_String ("/*");
   Closing_Pattern : Unbounded_String := To_Unbounded_String ("*/");
   Inside_Comment  : Boolean          := False;

   function Strip_Comments (From : String) return String is
      use Ada.Strings.Fixed;
      Opening_Index : Natural;
      Closing_Index : Natural;
      Start_Index   : Natural := From'First;
   begin
      if Inside_Comment then
         Start_Index :=
            Index (Source => From, Pattern => To_String (Closing_Pattern));
         if Start_Index < From'First then
            return "";
         end if;
         Inside_Comment := False;
         Start_Index    := Start_Index + Length (Closing_Pattern);
      end if;
      Opening_Index :=
         Index
           (Source  => From,
            Pattern => To_String (Opening_Pattern),
            From    => Start_Index);
      if Opening_Index < From'First then
         return From (Start_Index .. From'Last);
      else
         Closing_Index :=
            Index
              (Source  => From,
               Pattern => To_String (Closing_Pattern),
               From    => Opening_Index + Length (Opening_Pattern));
         if Closing_Index > 0 then
            return From (Start_Index .. Opening_Index - 1) &
                   Strip_Comments
                      (From (
               Closing_Index + Length (Closing_Pattern) .. From'Last));
         else
            Inside_Comment := True;
            return From (Start_Index .. Opening_Index - 1);
         end if;
      end if;
   end Strip_Comments;

   File : Ada.Text_IO.File_Type;
begin
   if Ada.Command_Line.Argument_Count < 1
     or else Ada.Command_Line.Argument_Count > 3
   then
      Print_Usage;
      return;
   end if;
   if Ada.Command_Line.Argument_Count > 1 then
      Opening_Pattern := To_Unbounded_String (Ada.Command_Line.Argument (2));
      if Ada.Command_Line.Argument_Count > 2 then
         Closing_Pattern :=
            To_Unbounded_String (Ada.Command_Line.Argument (3));
      else
         Closing_Pattern := Opening_Pattern;
      end if;
   end if;
   Ada.Text_IO.Open
     (File => File,
      Mode => Ada.Text_IO.In_File,
      Name => Ada.Command_Line.Argument (1));
   while not Ada.Text_IO.End_Of_File (File => File) loop
      declare
         Line : constant String := Ada.Text_IO.Get_Line (File);
      begin
         Ada.Text_IO.Put_Line (Strip_Comments (Line));
      end;
   end loop;
   Ada.Text_IO.Close (File => File);
end Strip;