Jump to content

RegEx a function with its parameters


argumentum
 Share

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

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   1 member

×
×
  • Create New...