ListIncludes.ahk Regex issue

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
daywalker
Posts: 32
Joined: 18 Jun 2019, 01:37

ListIncludes.ahk Regex issue

21 Feb 2023, 10:54

Hi,
i try to get all #includes with ListIncludes.ahk but only maincode.ahk is returned (msg.ahk is discarded):
image.png
image.png (4.16 KiB) Viewed 335 times

Here the (reduced) code i use:

Get_all_includes.ahk:

Code: Select all

#include ListIncludes.ahk
; ListIncludes.ahk from
; https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/ListIncludes.ahk
; https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/libs/g-n/ListIncludes.ahk

script_mainfilename:="maincode.ahk"
list := ListIncludes(script_mainfilename)
msgbox result:`n%list%
maincode.ahk:

Code: Select all

; ------------------------------  ScriptGuard1  --------------------------------
ScriptGuard1()                    ; Hides AutoHotkey source in compiled scripts
{ ; By TAC109, Edition: 28Jan2023 ; Include this code in your script at the top
  static _:=SubStr(A_AhkVersion,1,1)=1?ScriptGuard1():1 ;Runs when script starts
  local ahk:=">AUTOHOTKEY SCRIPT<", pt:=0,sz:=0,d:=0,k,v, rx:=0x7FFFFFFFFFFFFFFF
  ,rc, ahk1:="~AUTOHOTKEY SCRIPT~",rs:=0x7FFFFFFFFFFFFFFF,rz:=0x7FFFFFFFFFFFFFFF
  if A_IsCompiled ;^ Don't alter! ; See bit.ly/ScriptGuard for more details
  {	for k,v in [ahk1, ahk, "#1"]  ; Works with v1.1 & v2, but not _H
      if (rc:=DllCall("FindResource",  "Ptr",0, v ~= "^#\d$" ? "Ptr" : "Str", v
         ~= "^#\d$" ? SubStr(v,2) : v, "Ptr",10, "Ptr"))
      && (sz:=DllCall("SizeofResource","Ptr",0,  "Ptr",rc, "Uint"))
      && (pt:=DllCall("LoadResource",  "Ptr",0,  "Ptr",rc, "Ptr"))
      && (pt:=DllCall("LockResource",  "Ptr",pt, "Ptr"))
      && (DllCall("VirtualProtect","Ptr",pt, "Ptr",sz, "UInt",0x04, "UInt*",rc))
        DllCall("RtlZeroMemory","Ptr",pt, "Ptr",sz), d:=k ; Wipe script from RAM
    (rs=rx)?0:DllCall("VirtualProtect","Ptr",rs,"Ptr",rz,"UInt",0x02,"UInt*",rc)
    (d<2)?DllCall("MessageBox","Int",0,"Str","Warning: ScriptGuard1 not active!"
    . "`n`nError = " (A_LastError=1814 ? ("Resource Name '" ahk "' not found."
    . "`nTo fix, see the 'Example 1' comments at https://bit.ly/BinMod.")
    : A_LastError), "Str", A_ScriptName, "Int", 64):0 ; For additional security,
}	} (SubStr(A_AhkVersion,1,1) = 1) ? 0 : ScriptGuard1() ; see bit.ly/BinMod
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
#include msg.ahk

Outputfile=C:\temp\out.txt
FileAppend,
(
A line of text.
), %Outputfile%

if ErrorLevel
{
 ;print some error
}
msg.ahk:

Code: Select all

msgbox Hello
Debuging:
I suppose line#46 of ListIncludes.ahk is making the problem as it does the cutting of the round brackets "(" ")".
To debug i modfied ListIncludes.ahk and write out the variable script before and after the RegExReplace :

Code: Select all

     [...]
     
     Fileappend, %script%, C:\Temp\in.txt    ;code to add for debug

    ; code from Listincludes.ahk #lines43-46
    script := RegExReplace(script
        , "ms`a)^\s*/\*.*?^\s*\*/\s*"  ; multi-line comments
        . "|\s*(?<!\S);.*?$"           ; single-line comments
        . "|^\s*\(.*?^\s*\)\s*")       ; continuation sections
     
     Fileappend, %script%, C:\Temp\out.txt   ;code to add for debug

     [...]
You will see that some code - also the #include - is missing in the out.txt.

As i am not so firm with RegEx i would be glad for a helpful hand.

Thanks a lot!
Regards
daywalker
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: ListIncludes.ahk Regex issue

10 Aug 2023, 21:05

When ListIncludes was written, a line like this:

Code: Select all

(rs=rx)?0:DllCall("VirtualProtect","Ptr",rs,"Ptr",rz,"UInt",0x02,"UInt*",rc)
would have been interpreted as the start of a continuation section with invalid (ignored) options. ListIncludes ignores everything from there to the next line which starts with ), because it is designed to ignore lines in continuation sections.

To fix this, you can find the line

Code: Select all

        . "|^\s*\(.*?^\s*\)\s*")       ; continuation sections
and replace it with

Code: Select all

        . "|^\s*\((?i:Join[^ `t`r`n]*+|(?<![^ `t`r`n]);[^`r`n]*|[^ `t`r`n)]++|[ `t]++)*+\R"
            . ".*?^\s*\)\s*")
This is a modified version of the regex that the v2 launcher uses to detect continuation sections. It accounts for expressions starting with ( the same way that AutoHotkey does: by looking for a ) which isn't part of the Join option.

Technically \s matches more whitespace characters than those recognized by AutoHotkey, but it's unlikely to matter.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn and 195 guests