Jump to content

RegEx a function with its parameters


argumentum
 Share

Go to solution Solved by donnyh13,

Recommended Posts

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ''1''', "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF, 88, 1, 2) ;  don't add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, @ScriptLineNumber, 1, 2) ;  don't touch
Local $sString = "This is' a string"
ConsoleWriteDebug($sString & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug($sString, @ScriptLineNumber) ;         don't add @ScriptLineNumber
ConsoleWriteDebug($sString) ;   

_Add_ScriptLineNumber(FileRead(@ScriptFullPath))
Func _Add_ScriptLineNumber($sScript, $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    ; help coding this
    Return $sScript ; with the added ", @ScriptLineNumber)" where needed
EndFunc   ;==>_Add_ScriptLineNumber

Func ConsoleWriteDebug($sStr = @CRLF, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local $iRet = ConsoleWrite("@@ Debug (" & $iLine & ") : " & $sStr & (StringRight($sStr, 2) = @CRLF ? "" : @CRLF))
    Return SetError($iError, $iExtended, $iRet) ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1538974
EndFunc   ;==>ConsoleWriteDebug

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

..wrote this ConsoleWriteDebug() and would like to compile with "/rsln: Replace @ScriptLineNumber with the actual line number."
But is not magical, if the @ScriptLineNumber is not there, is just not there.
So I'd like to run a pre-processor that adds the string "@ScriptLineNumber" to the function at, in this case the 2nd argument, so that Au3Stripper can do it's thing.

Any of you RegEx gods feel like giving it a try ? TIA

Edit: I think I've got it in a post below.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • argumentum changed the title to RegEx a function with its parameters
Spoiler

..as far as I can take it:

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ''1''', "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF, 88, 1, 2) ;  don't add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, @ScriptLineNumber, 1, 2) ;  don't touch
Local $sString = "This is' a string"
ConsoleWriteDebug($sString & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug($sString, @ScriptLineNumber) ;         don't add @ScriptLineNumber
ConsoleWriteDebug($sString) ;                                  add @ScriptLineNumber


ConsoleWrite(@CRLF & _Add_ScriptLineNumber(FileRead(@ScriptFullPath)) & @CRLF)
Func _Add_ScriptLineNumber($sScript, $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    Local $aRegex, $iPos, $aScript = StringSplit($sScript, @CRLF, 1)
    $sScript = ""
    Local $i_Base, $i_apostrophe_1, $i_quotation_1, $i_comma_1
    For $n = 1 To UBound($aScript) - 1

        If StringInStr($aScript[$n], 'Func ConsoleWriteDebug(') Or _
                Not StringInStr($aScript[$n], 'ConsoleWriteDebug(') Or _
                StringInStr($aScript[$n], ';') < StringInStr($aScript[$n], 'ConsoleWriteDebug(') Then
            $sScript &= $aScript[$n] & @CRLF
            ContinueLoop
        EndIf

        $aRegex = StringRegExp($aScript[$n], 'ConsoleW' & 'riteDebug\(.*\)', 1)
        If UBound($aRegex) <> 1 Then
            $sScript &= $aScript[$n] & @CRLF
            ContinueLoop
        EndIf

        ConsoleWrite('- >' & '' & '<' & @TAB & $i_apostrophe_1 & @TAB & $i_quotation_1 & @TAB & $i_comma_1 & @CRLF)
        ConsoleWrite('@@  RegEx (' & $n & ') : ' & $aRegex[0] & @CRLF)

        $sRegex = StringTrimRight(StringReplace($aRegex[0], 'ConsoleWriteDebug(', ''), 1)
        $sSpecialChr = StringLeft($sRegex, 1)
        $aStringSplitSV = _StringSplitSV($sRegex, ",", $sSpecialChr)
        If UBound($aStringSplitSV) = 1 Then
            Switch $sSpecialChr
                Case '"', "'"
                    $sScript &= StringReplace($aScript[$n], $aRegex[0], 'ConsoleWriteDebug(' & $aStringSplitSV[0] & ', @ScriptLineNumber)') & " ;;; DONE ;;;" & @CRLF
                Case Else
                    $sScript &= $aScript[$n] & ' ;;; need to discern ;;;' & @CRLF
            EndSwitch
        ElseIf UBound($aStringSplitSV) > 1 Then
            $sScript &= $aScript[$n] & ' ;;; leave as is ;;;' & @CRLF
        Else
            $sScript &= $aScript[$n] & ' ;;; no clue ;;;' & @CRLF
        EndIf

        For $m = 0 To UBound($aStringSplitSV) - 1
            ConsoleWrite('>' & $m & @TAB & $aStringSplitSV[$m] & @CRLF)
        Next



    Next
    Return $sScript ; with the added ", @ScriptLineNumber)" where needed
EndFunc   ;==>_Add_ScriptLineNumber

Func ConsoleWriteDebug($sStr = @CRLF, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local $iRet = ConsoleWrite("@@ Debug (" & $iLine & ") : " & $sStr & (StringRight($sStr, 2) = @CRLF ? "" : @CRLF))
    Return SetError($iError, $iExtended, $iRet) ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1538974
EndFunc   ;==>ConsoleWriteDebug

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

Func _StringSplitSV($sString, $sSepChr = ",", $sSpecialChr = '"') ; https://www.autoitscript.com/forum/topic/105756-string-split-escapeignore-quoted-delimiters/?do=findComment&comment=747333
    Return StringRegExp($sString, "\G(?:\Q" & $sSepChr & "\E|^)((?>[^\Q" & $sSepChr & $sSpecialChr & "\E]*(?:" & $sSpecialChr & "[^\Q" & $sSpecialChr & "\E]*" & $sSpecialChr & ")?)+)", 3)
EndFunc   ;==>_StringSplitSV

Hid the post. No longer relevant.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

:sorcerer:

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ''1''', "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF, 88, 1, 2) ;  don't add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, @ScriptLineNumber, 1, 2) ;  don't touch
Local $sString = "This is' a string"
ConsoleWriteDebug($sString & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug($sString, @ScriptLineNumber) ;         don't add @ScriptLineNumber
ConsoleWriteDebug($sString) ;                                  add @ScriptLineNumber

ConsoleWrite(@CRLF & _Add_ScriptLineNumber(FileRead(@ScriptFullPath)) & @CRLF)

Func _Add_ScriptLineNumber00($sScript, $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    Local $aRegex, $iPos, $aScript = StringSplit($sScript, @CRLF, 1)
    $sScript = ""
    Local $i_Base, $i_apostrophe_1, $i_quotation_1, $i_comma_1
    For $n = 1 To UBound($aScript) - 1

        If StringInStr($aScript[$n], 'Func ConsoleWriteDebug(') Or _
                Not StringInStr($aScript[$n], 'ConsoleWriteDebug(') Or _
                StringInStr($aScript[$n], ';') < StringInStr($aScript[$n], 'ConsoleWriteDebug(') Then
            $sScript &= $aScript[$n] & @CRLF
            ContinueLoop
        EndIf

        $aRegex = StringRegExp($aScript[$n], 'ConsoleW' & 'riteDebug\(.*\)', 1)
        If UBound($aRegex) <> 1 Then
            $sScript &= $aScript[$n] & @CRLF
            ContinueLoop
        EndIf

        ConsoleWrite('- >' & '' & '<' & @TAB & $i_apostrophe_1 & @TAB & $i_quotation_1 & @TAB & $i_comma_1 & @CRLF)
        ConsoleWrite('@@  RegEx (' & $n & ') : ' & $aRegex[0] & @CRLF)

        $sRegex = StringTrimRight(StringReplace($aRegex[0], 'ConsoleWriteDebug(', ''), 1)
        $sSpecialChr = StringLeft($sRegex, 1)
        $aStringSplitSV = _StringSplitSV($sRegex, ",", $sSpecialChr)
        If UBound($aStringSplitSV) = 1 Then
            Switch $sSpecialChr
                Case '"', "'"
                    $sScript &= StringReplace($aScript[$n], $aRegex[0], 'ConsoleWriteDebug(' & $aStringSplitSV[0] & ', @ScriptLineNumber)') & " ;;; DONE ;;;" & @CRLF
                Case Else
                    $sScript &= $aScript[$n] & ' ;;; need to discern ;;;' & @CRLF
            EndSwitch
        ElseIf UBound($aStringSplitSV) > 1 Then
            $sScript &= $aScript[$n] & ' ;;; leave as is ;;;' & @CRLF
        Else
            $sScript &= $aScript[$n] & ' ;;; no clue ;;;' & @CRLF
        EndIf

        For $m = 0 To UBound($aStringSplitSV) - 1
            ConsoleWrite('>' & $m & @TAB & $aStringSplitSV[$m] & @CRLF)
        Next

    Next
    Return $sScript ; with the added ", @ScriptLineNumber)" where needed
EndFunc   ;==>_Add_ScriptLineNumber

Func ConsoleWriteDebug($sStr = @CRLF, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local $iRet = ConsoleWrite("@@ Debug (" & $iLine & ") : " & $sStr & (StringRight($sStr, 2) = @CRLF ? "" : @CRLF))
    Return SetError($iError, $iExtended, $iRet) ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1538974
EndFunc   ;==>ConsoleWriteDebug

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

Func _StringSplitSV($sString, $sSepChr = ",", $sSpecialChr = '"') ; https://www.autoitscript.com/forum/topic/105756-string-split-escapeignore-quoted-delimiters/?do=findComment&comment=747333
    Return StringRegExp($sString, "\G(?:\Q" & $sSepChr & "\E|^)((?>[^\Q" & $sSepChr & $sSpecialChr & "\E]*(?:" & $sSpecialChr & "[^\Q" & $sSpecialChr & "\E]*" & $sSpecialChr & ")?)+)", 3)
EndFunc   ;==>_StringSplitSV

#Region ; ===( GPT solution )===
Func _Add_ScriptLineNumber($sScript, $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    Local $aScript = StringSplit($sScript, @CRLF, 1), $sProcessedScript = ""

    ; Define the regex to match ConsoleWriteDebug with its parameters
    Local $sRegex = '(?i)\b' & $sFuncName & '\((.*)\)'

    For $i = 1 To $aScript[0]
        Local $sLine = $aScript[$i]

        ; Skip if the line does not contain the function
        If Not StringInStr($sLine, $sFuncName & '(') Then
            $sProcessedScript &= $sLine & @CRLF
            ContinueLoop
        EndIf

        ; Extract the parameters using RegExp
        Local $aMatch = StringRegExp($sLine, $sRegex, 1)
        If @error Or UBound($aMatch) = 0 Then
            $sProcessedScript &= $sLine & @CRLF
            ContinueLoop
        EndIf

        ; Parse the parameters
        Local $aParams = StringSplit($aMatch[0], ',', 1)
        If $aParams[0] < $iAddAtParameterNumber Then
            ; Add @ScriptLineNumber if not enough parameters
            $sLine = StringReplace($sLine, ")", ", @ScriptLineNumber)")
        ElseIf Not StringInStr($aParams[$iAddAtParameterNumber], "@ScriptLineNumber") Then
            ; Insert @ScriptLineNumber if missing in the target parameter
            $aParams[$iAddAtParameterNumber] = "@ScriptLineNumber"
            $sLine = $sFuncName & "(" & _JoinParams($aParams) & ")"
        EndIf

        $sProcessedScript &= $sLine & @CRLF
    Next

    Return $sProcessedScript
EndFunc   ;==>_Add_ScriptLineNumber

; Helper function to join parameters
Func _JoinParams($aParams)
    Local $sJoined = ""
    For $i = 1 To $aParams[0]
        $sJoined &= ($i > 1 ? ", " : "") & $aParams[$i]
    Next
    Return $sJoined
EndFunc   ;==>_JoinParams
#EndRegion ; ===( GPT solution )===

 

Edited by ioa747
add missing part

I know that I know nothing

Link to comment
Share on other sites

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ''1''', "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;  add @ScriptLineNumber

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

Nice @ioa747. I bet you removed anyFunc() because it wasn't there ( in the 1st post ), or didn't refresh this posting's page.
In any case,  in a ConsoleWrite() when you run a function inside and you'd wanna see the function's output your current code brakes the output script.
I do that ( run func() inside a ConsoleWrite() ) a lot.

I was adding stuff as I went along,  looking to brake the code with less than ideal circumstances, like having commas or parenthesis or some other character that would confuse the function that adds the @ScriptLineNumber to the script.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ;''1''', "2""", @ScriptLineNumber) & @CRLF, 3) ;  don't add @ScriptLineNumber
ConsoleWriteDebug(anyFunc(' ;''1''', "2""", @ScriptLineNumber) & @CRLF) ;     add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug("This, ;is' a string" & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug('This, ;is'' a string' & @CRLF, @ScriptLineNumber) ;  don't add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF, 88, 1, 2) ;             don't add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, @ScriptLineNumber, 1, 2) ;  don't touch
Local $sString = "This is' a string"
ConsoleWriteDebug($sString & @CRLF, 11) ; don't add @ScriptLineNumber
ConsoleWriteDebug($sString, 12) ;         don't add @ScriptLineNumber
ConsoleWriteDebug($sString) ;             add @ScriptLineNumber
ConsoleWriteDebug($sString & @CRLF) ;             add @ScriptLineNumber


ConsoleWrite(@CRLF & _Add_ScriptLineNumber(FileRead(@ScriptFullPath)) & @CRLF)
Func _Add_ScriptLineNumber($sScript) ; , $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    Local $aRegex, $iPos, $aScript = StringSplit($sScript, @CRLF, 1)
    $sScript = ""
    Local $i_Base, $i_apostrophe_1, $i_quotation_1, $i_comma_1
    For $n = 1 To UBound($aScript) - 1

        If StringInStr($aScript[$n], 'Func ConsoleW' & 'riteDebug(') Or _
                Not StringInStr($aScript[$n], 'ConsoleW' & 'riteDebug(') Or _
                StringInStr($aScript[$n], ';') < StringInStr($aScript[$n], 'ConsoleW' & 'riteDebug(') Then
            $sScript &= $aScript[$n] & @CRLF
            ContinueLoop
        EndIf

        ConsoleWrite('=== === === === === === === === === === === === === === === === ' & @CRLF)

        $aRegex = StringRegExp($aScript[$n], 'ConsoleW' & 'riteDebug\(.*\)', 1)
        For $m = 0 To UBound($aRegex) - 1
            ConsoleWrite('@@ $aRegex (' & @ScriptLineNumber & ') : [' & $m & '] >' & $aRegex[$m] & '<' & @CRLF)
        Next
        If UBound($aRegex) <> 1 Then
            $sScript &= $aScript[$n] & ' ;;; skipped 4 ;;;' & @CRLF
            ConsoleWrite('> skipped' & @CRLF)
            ContinueLoop
        EndIf

        If StringInStr($aRegex[0], "ConsoleW' & 'riteDebug($") Then ; on variable
            If StringInStr($aRegex[0], ",") Then
                $sScript &= $aScript[$n] & ' ;;; skipped 3 ;;;' & @CRLF
                ConsoleWrite('> skipped 3' & @CRLF)
            Else
                $sScript &= StringReplace($aScript[$n], $aRegex[0], StringTrimRight($aRegex[0], 1) & ', @ScriptLineNumber)') & " ;;; DONE 3 ;;;" & @CRLF
                ConsoleWrite('> DONE 3' & @CRLF)
            EndIf
            ContinueLoop
        EndIf

        If StringInStr($aRegex[0], ")", 0, -2) Then ; on function
            If StringInStr($aRegex[0], ",", 0, -1) > StringInStr($aRegex[0], ")", -2) Then
                $sScript &= $aScript[$n] & ' ;;; skipped 4 ;;;' & @CRLF
                ConsoleWrite('> skipped 4' & @CRLF)
            Else
                $sScript &= StringReplace($aScript[$n], $aRegex[0], StringTrimRight($aRegex[0], 1) & ', @ScriptLineNumber)') & " ;;; DONE 4 ;;;" & @CRLF
                ConsoleWrite('> DONE 4' & @CRLF)
            EndIf
            ContinueLoop
        EndIf

        $sRegex = StringTrimRight(StringReplace($aRegex[0], 'ConsoleW' & 'riteDebug(', ''), 1)
        $sSpecialChr = StringLeft($sRegex, 1)
        $aStringSplitSV = _StringSplitSV($sRegex, ",", $sSpecialChr)

;~      For $m = 0 To UBound($aStringSplitSV) - 1
;~          ConsoleWrite('@@ SplitSV (' & @ScriptLineNumber & ') : [' & $m & '] >' & $aStringSplitSV[$m] & '<' & @CRLF)
;~      Next


        If UBound($aStringSplitSV) = 1 Then
            Switch $sSpecialChr
                Case '"', "'"
                    $sScript &= StringReplace($aScript[$n], $aRegex[0], 'ConsoleW' & 'riteDebug(' & $aStringSplitSV[0] & ', @ScriptLineNumber)') & " ;;; DONE 1 ;;;" & @CRLF
                    ConsoleWrite('> DONE 1' & @CRLF)
                Case Else
                    $sScript &= $aScript[$n] & ' ;;; need to discern ;;;' & @CRLF
                    ConsoleWrite('> need to discern' & @CRLF)
            EndSwitch
        ElseIf UBound($aStringSplitSV) > 1 Then
            $sScript &= $aScript[$n] & ' ;;; skipped 2 ;;;' & @CRLF
            ConsoleWrite('> skipped 2' & @CRLF)
        Else
            $sScript &= $aScript[$n] & ' ;;; no clue ;;;' & @CRLF
            ConsoleWrite('> no clue' & @CRLF)
        EndIf

    Next
    ConsoleWrite('=== === === === === === === === === === === === === === === === finished.' & @CRLF)
    Return $sScript ; with the added ", @ScriptLineNumber)" where needed
EndFunc   ;==>_Add_ScriptLineNumber

Func ConsoleWriteDebug($sStr = @CRLF, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local $iRet = ConsoleWrite("@@ Debug (" & $iLine & ") : " & $sStr & (StringRight($sStr, 2) = @CRLF ? "" : @CRLF))
    Return SetError($iError, $iExtended, $iRet) ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1538974
EndFunc   ;==>ConsoleWriteDebug

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

Func _StringSplitSV($sString, $sSepChr = ",", $sSpecialChr = '"') ; https://www.autoitscript.com/forum/topic/105756-string-split-escapeignore-quoted-delimiters/?do=findComment&comment=747333
    Return StringRegExp($sString, "\G(?:\Q" & $sSepChr & "\E|^)((?>[^\Q" & $sSepChr & $sSpecialChr & "\E]*(?:" & $sSpecialChr & "[^\Q" & $sSpecialChr & "\E]*" & $sSpecialChr & ")?)+)", 3)
EndFunc   ;==>_StringSplitSV

ok, this does it.

If any one can either brake it or make it better, post :) 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Solution

I started on this before you found your above solution, but never had time to finish. I thought I might as well share it now since I completed it. It’s a bit more complex looking than yours, and can also be broken a few different ways. But it does works for the examples you posted, here it is.

#include <StringConstants.au3>

; looking to discern the parameters with RegExp

ConsoleWriteDebug(anyFunc(' ''1''', "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF) ;   add @ScriptLineNumber
ConsoleWriteDebug('This, is'' a string' & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug("This, is' a string" & @CRLF, 88, 1, 2) ;  don't add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, @ScriptLineNumber, 1, 2) ;  don't touch
Global $sString = "This is' a string"
ConsoleWriteDebug($sString & @CRLF, @ScriptLineNumber) ; don't add @ScriptLineNumber
ConsoleWriteDebug($sString, @ScriptLineNumber) ;         don't add @ScriptLineNumber
ConsoleWriteDebug($sString) ;

ConsoleWriteDebug(anyFunc(anyFunc(' ''1''', "2""", @ScriptLineNumber), "2""", @ScriptLineNumber) & @CRLF) ;  add @ScriptLineNumber
ConsoleWriteDebug("10", "2") ;  add @ScriptLineNumber
ConsoleWriteDebug(-1, "2") ;  add @ScriptLineNumber
anyFunc(ConsoleWriteDebug("10")) ;  add @ScriptLineNumber
;~ ConsoleWriteDebug("This is' a string" & @CRLF, 123, 1, 2) ;  don't touch


ConsoleWrite(_Add_ScriptLineNumber(FileRead(@ScriptFullPath)) & @CRLF)
Func _Add_ScriptLineNumber($sScript, $sFuncName = "ConsoleWriteDebug", $iAddAtParameterNumber = 2)
    Local $sFixedScript = "", $sLine, $sAddCRLF = @CRLF, $sEmptyAddParam = '""'
    Local $asScript[0], $asMatch[0]
    Local Const $iQuoteChar = 34
    Local $iParam = 1, $iFuncLevel = 1, $iLine = 0

    ConsoleWrite("> =========== BEGINNING ===========" & @CRLF)

    $asScript = StringSplit($sScript, @CRLF, $STR_ENTIRESPLIT)

    For $i = 1 To $asScript[0]
        $sLine = $asScript[$i]
        $iLine += 1

        If ($i = $asScript[0]) Then $sAddCRLF = ""

        If StringRegExp($sLine, "\Q" & $sFuncName & "\E\(") And Not StringRegExp($sLine, "^(?:[\s]*;|[\s]*\b[fF][uU][nN][cC]\b[ ]|[\s]*\b[eE][nN][dD][fF][uU][nN][cC]\b[ ])") Then ; If function is in the line, and not commented out, and not the Func declaration itself.
            $iParam = 1
            $iFuncLevel = 1

            $asMatch = StringRegExp($sLine, ".*\Q" & $sFuncName & "\E\([/s]*", $STR_REGEXPARRAYMATCH)
            _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

            Do

                If ($iParam = $iAddAtParameterNumber) And ($iFuncLevel = 1) Then

                    If StringRegExp($sLine, "^[\d]") Then ; Raw digit in position.

                        $asMatch = StringRegExp($sLine, "^[\d]*", $STR_REGEXPARRAYMATCH)
                        If IsArray($asMatch) And ($iLine <> $asMatch[0]) Then ConsoleWrite("! Line " & $iLine & ", Raw line number is incorrect! Current = " & $asMatch[0] & "; Should be: " & $iLine & @CRLF)

                    ElseIf StringRegExp($sLine, "^\Q@ScriptLineNumber\E") Then ; @ScriptLineNumber in position, do nothing.

                    Else
                        $asMatch = StringRegExp($sLine, "^.+?(?:\)|,)", $STR_REGEXPARRAYMATCH) ; Retrieve data to next comma or end parenthesis.

                        If IsArray($asMatch) Then

                            If StringRegExp($asMatch[0], "[^\d\), " & Chr($iQuoteChar) & "]") Then ; See if parameter contains anything other than digits, quotes and/or spaces.
                                ConsoleWrite("- Line " & $iLine & ", Encountered parameter not containing @ScriptLineNumber and no line number alone:" & @TAB & $sLine & @CRLF)

                            ElseIf StringRegExp($asMatch[0], "[\d]") Then ; See if parameter contains digits.
                                $asMatch = StringRegExp($sLine, "[\d]+", $STR_REGEXPARRAYMATCH)
                                If IsArray($asMatch) And ($iLine <> $asMatch[0]) Then ConsoleWrite("! Line " & $iLine & ", Raw quoted line number is incorrect! Current = " & $asMatch[0] & "; Should be: " & $iLine & @CRLF)

                            ElseIf Not StringRegExp($asMatch[0], "\Q@ScriptLineNumber\E") Then
                                ConsoleWrite("- Line " & $iLine & ", Encountered parameter not containing @ScriptLineNumber:" & @TAB & $sLine & @CRLF)

                            Else
                                ConsoleWrite("- Line " & $iLine & ", Encountered unknown parameter in @ScriptLineNumber position:" & @TAB & $sLine & @CRLF)
                            EndIf
                        EndIf
                    EndIf

                    $sFixedScript &= $sLine & $sAddCRLF
                    ExitLoop
                EndIf

                Select

                    Case StringRegExp($sLine, "^[a-zA-Z_]") ; A function as it starts with a letter or underscore.
                        $asMatch = StringRegExp($sLine, "^[\w]+\(?[ ]*", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                        $iFuncLevel += 1 ; Show I am in a function in a Parameter, and not the main ConsoleWriteDebug function.

                    Case StringRegExp($sLine, "^[\[\(]") ; A Bracket.

                        $asMatch = StringRegExp($sLine, "^((?:\(.+?\)|\[.+?\])[\s]*)", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)
;~
                    Case StringRegExp($sLine, "^\$") ; A Variable.
                        $asMatch = StringRegExp($sLine, "(^\$[\w\[\]]*[\s]*)", $STR_REGEXPARRAYMATCH) ; can be broken with spaces in array brackets $aArray[ 0]
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                    Case StringRegExp($sLine, "^['" & Chr($iQuoteChar) & "]") ; A String.

                        $asMatch = StringRegExp($sLine, "^((?:'.*?'|" & Chr($iQuoteChar) & ".*?" & Chr($iQuoteChar) & ")[\s]*)", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                    Case StringRegExp($sLine, "^[@&]") ; A Macro or joiner.

                        $asMatch = StringRegExp($sLine, "^((?:@|&)[\w]*[\s]*)", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                    Case StringRegExp($sLine, "^[\-\d]") ; A digit, including a negative.

                        $asMatch = StringRegExp($sLine, "^([\-\d,\.]+[\s]*)", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                    Case StringRegExp($sLine, "^\)") ; An ending parenthesis.
                        If ($iFuncLevel = 1) Then ; I'm in the main ConsoleWriteDebug function, determine if I need to add @ScriptLineNumber. Else I'm in a function in a function, just decrease count and process.

                            If ($iParam < ($iAddAtParameterNumber - 1)) Then ; Minus 1 to compensate for @ScriptLineNumber parameter being added.

                                While ($iParam < ($iAddAtParameterNumber - 1)) ; If Parameter to fill in @ScriptLineNumber isn't current one, add blanks
                                    $sFixedScript &= ", " & $sEmptyAddParam
                                    $iParam += 1
                                WEnd

                            EndIf
                            ConsoleWrite("- Adding @ScriptLineNumber to line: " & $iLine & @CRLF)
                            $sFixedScript &= ", " & "@ScriptLineNumber" & $sLine & $sAddCRLF

                            ExitLoop
                        Else
                            $iFuncLevel -= 1

                            $asMatch = StringRegExp($sLine, "^\)[\s]*", $STR_REGEXPARRAYMATCH)
                            _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                        EndIf

                    Case StringRegExp($sLine, "^,") ; A comma.

                        If ($iFuncLevel = 1) Then $iParam += 1 ; I'm in the main ConsoleWriteDebug function, increase Parameter count. Else I'm in a function in a function, just process.

                        $asMatch = StringRegExp($sLine, "^,[\s]*", $STR_REGEXPARRAYMATCH)
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                    Case Else ; Unexpected character, trim one off
                        ConsoleWrite("> Encountered an unexpected character, trimming it off: " & StringLeft($sLine, 1) & @CRLF)
                        $sFixedScript &= StringLeft($sLine, 1) ; add the removed character to the script string.
                        $sLine = StringTrimLeft($sLine, 1)

                        $asMatch = StringRegExp($sLine, "^[\s]*", $STR_REGEXPARRAYMATCH) ; Remove any white space.
                        _AddLine($sFixedScript, $sLine, $asMatch, $iLine, $sAddCRLF)

                EndSelect

            Until $sLine = ""

        Else ; Just copy the line.
            $sFixedScript &= $sLine & $sAddCRLF

        EndIf

    Next

    ConsoleWrite("+ =========== FINISHED ===========" & @CRLF)

    Return $sFixedScript ; with the added ", @ScriptLineNumber)" where needed
EndFunc   ;==>_Add_ScriptLineNumber

Func _AddLine(ByRef $sFixedScript, ByRef $sLine, ByRef $asArray, $iLine, $sAddCRLF)

    If Not IsArray($asArray) Then
        ConsoleWrite("! Identification failed on line: " & $iLine & " Skipping" & @CRLF)
        $sFixedScript &= $sLine & $sAddCRLF
        $sLine = ""
    Else
        $sFixedScript &= $asArray[0] ; add the beginning of the string to the script string.

        $sLine = StringTrimLeft($sLine, StringLen($asArray[0]))

    EndIf

EndFunc   ;==>_AddLine


Func ConsoleWriteDebug($sStr = @CRLF, $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local $iRet = ConsoleWrite("@@ Debug (" & $iLine & ") : " & $sStr & (StringRight($sStr, 2) = @CRLF ? "" : @CRLF))
    Return SetError($iError, $iExtended, $iRet) ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1538974
EndFunc   ;==>ConsoleWriteDebug

Func anyFunc($1 = "", $2 = "", $3 = "")
    Return 'anyFunc' & $1 & $2 & $3
EndFunc   ;==>anyFunc

Can be broken if:

  • A parameter contains an Array variable with spaces in the element $aArray[ 0]
  • If ConsoleWriteDebug is commented out, but the comment mark (;) is not the first Character (besides whitespaces).

Benefits:

  • Works when ConsoleWriteDebug is nested.
  • Works when functions in ConsoleWriteDebug are nested, i.e. ConsoleWriteDebug(anyFunc(anyFunc(
  • Checks for accuracy raw line numbers.
  • Can place @ScriptLineNumber in different Parameter place, filling in skipped parameters with a predesignated string.

Maybe it will give some options. :)

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

38 minutes ago, donnyh13 said:

Can be broken if:

Found the "If ConsoleWriteDebug is commented out, but the comment mark (;) is not the first Character (besides whitespaces)."
Could not find the "A parameter contains an Array variable with spaces in the element $aArray[ 0]"

Knowing how to brake it, lets me know what not to do. Can you show how to brake it ?  :) 
Just a line is all I need

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

6 minutes ago, argumentum said:

Can you show how to brake it ?

Okay, It still processes correctly, but throws a warning, "> Encountered an unexpected character, trimming it off: ]"

Try this ; it will still process, but ... not processing under the correct section (as a variable), but probably in pieces? :)   Could cause unexpected behaviour?

Global $aArray[1] = [10]
ConsoleWriteDebug($aArray[ 0]) ;  add @ScriptLineNumber

Could use improving to prevent that probably. Will have to think on solutions.

Edited by donnyh13

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

Modifying the variable Case (Line 98~) from

$asMatch = StringRegExp($sLine, "(^\$[\w\[\]]*[\s]*)", $STR_REGEXPARRAYMATCH) ; can be broken with spaces in array brackets $aArray[ 0]

To

$asMatch = StringRegExp($sLine, "(^\$[\w]*[\s]*)", $STR_REGEXPARRAYMATCH)

I think will fix it.

Edited by donnyh13

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...