I am trying to create a VBA function that parses VBA code. I'm at the stage where I'm trying to put in an array variable all the instructions present on a line of code. For example the following code contains two instructions:
strVar = "Some text"
lngVar = 2
Those 2 instructions can also be written as following:
strVar = "Some text": lngVar = 2
I specify that I already have a personal function that transforms a "multiline" line into a simple line:
strVar = "From text": _
lngVar = 2 'Multiline'
So the CodeLine argument (the line of code to be parsed) of my custom function always contains a single code line. I thought I had reached the desired result since I get the right result with some twisted lines such as :
str = " : 1": str = " : 2": str = " : 2"
or
str = """ : 1""": str = """ : 2""": str = """ :"" 2""
But I realize that I don't get the desired result for this kind of lines:
Next vntSubString: CommentPosition = IIf(blnStringMode, 0, InStr(1, CodeLine, ": "))
I'd really like to do something generic and quick to execute, I think I'm not too far from the final result, but I'm a bit blocked.
Here's what my personal function looks like at the moment:
Public Function SplitInstructions(ByVal CodeLine As String) As Variant()
Dim vntResult() As Variant
Dim vntSubString¹ As Variant
Dim blnIsStringMode As Boolean 'Determines if we are in a subtext between quotes or not
Dim vntSubString² As Variant
Let vntResult = VBA.Array
If InStr(1, CodeLine, ": ") = 0 Then 'A single instruction
Let vntResult = VBA.Array(CodeLine)
ElseIf InStr(1, CodeLine, """") = 0 Then 'Several statements, but no quotes => On Split
Do Until VBA.InStr(1, CodeLine, "::") = 0
Let CodeLine = VBA.Replace(CodeLine, "::", ":")
Loop: Call AddToArray(vntResult, Split(CodeLine, ":"))
Else 'it gets complicated
For Each vntSubString¹ In Split(CodeLine, """")
If blnIsStringMode Then
Let vntResult(UBound(vntResult)) = Trim$(vntResult(UBound(vntResult)) & """" & vntSubString¹ & """")
Else
For Each vntSubString² In Split(vntSubString¹, ": ")
If vntSubString² <> vbNullString Then Call AddToArray(vntResult, vntSubString²)
Next vntSubString²
End If
Let blnIsStringMode = Not blnIsStringMode
Next vntSubString¹
End If
Let SplitInstructions = vntResult
End Function
Private Sub AddToArray(ByRef Arr() As Variant, ByVal Value As Variant)
Dim vntValue As Variant
If VBA.IsArray(Value) Then
For Each vntValue In Value
Call AddToArray(Arr, vntValue)
Next vntValue
Else
ReDim Preserve Arr(LBound(Arr) To UBound(Arr) + 1)
Let Arr(UBound(Arr)) = Value
End If
End Sub
In advance, thank you for your help!
Edit : here is the function I use to determine then comment position of a code line
Private Function CommentPosition(ByVal CodeLine As String) As Long
Dim vntSubString As Variant
Dim blnStringMode As Boolean
Dim x As Long
For Each vntSubString In VBA.Split(CodeLine, """")
If Not blnStringMode Then
Let x = VBA.InStr(1, vntSubString, "'")
If x > 0 Then
Let CommentPosition = CommentPosition + x
Exit Function
End If
End If
Let blnStringMode = Not blnStringMode
Let CommentPosition = CommentPosition + VBA.Len(vntSubString) + 1
Next vntSubString
Let CommentPosition = VBA.IIf(blnStringMode, 0, VBA.InStr(1, CodeLine, "'"))
End Function