Strip block comments: Difference between revisions

Content added Content deleted
m (Fixes PHP solution error)
Line 2,216: Line 2,216:
</pre>
</pre>


=={{header|VBA}}==
Stripping block comments might look easy ...
<lang vb>'and what if there are string literals which contaion delimeter characters?
'------------------------
'Delimeters for Block Comment can be specified, exactly two characters each
'Simple parser
'Three states: Block_Comment, String_Literal, Other_Text
Dim t As String 'target string
Dim s() As Byte 'source array
Dim j As Integer 'index into the source string s, converted to byte array
Dim SourceLength As Integer 'of a base 0 array, so last byte is SourceLength - 1
Dim flag As Boolean
Private Sub Block_Comment(sClBC As String)
'inside a block comment, expecting close block comment delimeter
flag = False
Do While j < SourceLength - 2
If s(j) = Asc(Left(sClBC, 1)) Then
If s(j + 1) = Asc(Right(sClBC, 1)) Then
'close block comment delimeter found
flag = True
j = j + 2
Exit Do
End If
'just a lone star
End If
j = j + 1
Loop
If Not flag Then MsgBox "Error, missing close block comment delimeter"
End Sub
Private Sub String_Literal()
'inside a string literal, expecting double quote as delimeter
flag = False
Do While j < SourceLength - 2
If s(j) = Asc("""") Then
If s(j + 1) = Asc("""") Then
'found a double quote within a string literal
t = t + Chr(s(j))
j = j + 1
Else
'close string literal delimeter found
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
'open block comment delimeter found
j = j + 2
Block_Comment 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" & 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
'VBA converts two double quotes in a row within a string literal to a single double quote
'see the output below. Quadruple double quotes become two double quotes within the string
'to represent a single double quote within a string.
n = n & "function subroutine() {" & vbCrLf
n = n & "a = /* inline comment */ b + c ;" & vbCrLf
n = n & "}" & vbCrLf
n = n & "/*/ <-- tricky 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 'The open and close delimeters for block comment are optional ;)
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</lang>{{out}}<pre>Original text:
------------------------------------------------------------
/**
* Some comments
* longer comments here that we can parse.
*
* Rahoo
*/
mystring = "This ""/*"" open comment block mark."
function subroutine() {
a = /* inline comment */ b + c ;
}
/*/ <-- tricky comments */

/**
* Another comment.
*/
function something() {
}

Text after deleting comment blocks, preserving string literals:
------------------------------------------------------------

mystring = "This ""/*"" open comment block mark."
function subroutine() {
a = b + c ;
}



function something() {
}</pre>
=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn stripper(text, a="/*", b="*/"){
<lang zkl>fcn stripper(text, a="/*", b="*/"){