Strip block comments: Difference between revisions

Content added Content deleted
(→‎{{header|VBA}}: handle nested comments as well)
Line 2,218: Line 2,218:
=={{header|VBA}}==
=={{header|VBA}}==
Stripping block comments might look easy ...
Stripping block comments might look easy ...
<lang vb>'strip block comment NESTED comments
<lang vb>'and what if there are string literals which contaion delimeter characters?
'multi line comments
'and what if there are string literals with these delimeters?
'------------------------
'------------------------
'Delimeters for Block Comment can be specified, exactly two characters each
'delimeters for Block Comment can be specified, exactly two characters each
'Simple parser
'Three states: Block_Comment, String_Literal, Other_Text
'Three states: Block_Comment, String_Literal, Other_Text
'Globals:
Dim t As String 'target string
Dim t As String 'target string
Dim s() As Byte 'source array
Dim s() As Byte 'source array
Line 2,228: Line 2,230:
Dim SourceLength As Integer 'of a base 0 array, so last byte is SourceLength - 1
Dim SourceLength As Integer 'of a base 0 array, so last byte is SourceLength - 1
Dim flag As Boolean
Dim flag As Boolean
Private Sub Block_Comment(sClBC As String)
Private Sub Block_Comment(sOpBC As String, sClBC As String)
'inside a block comment, expecting close block comment delimeter
'inside a block comment, expecting close block comment delimeter
flag = False
flag = False
Do While j < SourceLength - 2
Do While j < SourceLength - 2
If s(j) = Asc(Left(sClBC, 1)) Then
Select Case s(j)
If s(j + 1) = Asc(Right(sClBC, 1)) Then
Case Asc(Left(sOpBC, 1))
'close block comment delimeter found
If s(j + 1) = Asc(Right(sOpBC, 1)) Then
flag = True
'open block NESTED comment delimeter found
j = j + 2
j = j + 2
Exit Do
Block_Comment sOpBC, sClBC
End If
End If
'just a lone star
Case Asc(Left(sClBC, 1))
If s(j + 1) = Asc(Right(sClBC, 1)) Then
End If
'close block comment delimeter found
flag = True
j = j + 2
Exit Do
End If
'just a lone star
Case Else
End Select
j = j + 1
j = j + 1
Loop
Loop
Line 2,246: Line 2,256:
End Sub
End Sub
Private Sub String_Literal()
Private Sub String_Literal()
'inside a string literal, expecting double quote as delimeter
'inside as string literal, expecting double quote as delimeter
flag = False
flag = False
Do While j < SourceLength - 2
Do While j < SourceLength - 2
Line 2,288: Line 2,298:
'open block comment delimeter found
'open block comment delimeter found
j = j + 2
j = j + 2
Block_Comment sClBC
Block_Comment sOpBC, sClBC
Else
Else
t = t + Chr(s(j))
t = t + Chr(s(j))
Line 2,303: Line 2,313:
Dim n As String
Dim n As String
n = n & "/**" & vbCrLf
n = n & "/**" & vbCrLf
n = n & "* Some comments" & vbCrLf
n = n & "* Some comments /*NESTED COMMENT*/" & vbCrLf
n = n & "* longer comments here that we can parse." & vbCrLf
n = n & "* longer comments here that we can parse." & vbCrLf
n = n & "*" & vbCrLf
n = n & "*" & vbCrLf
Line 2,315: Line 2,325:
n = n & "a = /* inline comment */ b + c ;" & vbCrLf
n = n & "a = /* inline comment */ b + c ;" & vbCrLf
n = n & "}" & vbCrLf
n = n & "}" & vbCrLf
n = n & "/*/ <-- tricky comments */" & vbCrLf
n = n & "/*/ <-- tricky /*NESTED*/ comments */" & vbCrLf
n = n & "" & vbCrLf
n = n & "" & vbCrLf
n = n & "/**" & vbCrLf
n = n & "/**" & vbCrLf
Line 2,336: Line 2,346:
------------------------------------------------------------
------------------------------------------------------------
/**
/**
* Some comments
* Some comments /*NESTED COMMENT*/
* longer comments here that we can parse.
* longer comments here that we can parse.
*
*
* Rahoo
* Rahoo
*/
*/
mystring = "This ""/*"" open comment block mark."
mystring = "This is the ""/*"" open comment block mark."
function subroutine() {
function subroutine() {
a = /* inline comment */ b + c ;
a = /* inline comment */ b + c ;
}
}
/*/ <-- tricky comments */
/*/ <-- tricky /*NESTED*/ comments */


/**
/**
Line 2,356: Line 2,366:
------------------------------------------------------------
------------------------------------------------------------


mystring = "This ""/*"" open comment block mark."
mystring = "This is the ""/*"" open comment block mark."
function subroutine() {
function subroutine() {
a = b + c ;
a = b + c ;
Line 2,365: Line 2,375:
function something() {
function something() {
}</pre>
}</pre>

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