Skip to content

Commit

Permalink
Fix V1 to V2 label name conversion mmikeww#48, mmikeww#201
Browse files Browse the repository at this point in the history
Fix V1 to V2 label name conversion.
Does not address variable names, that will be handled separately
  • Loading branch information
andymbody committed Jul 7, 2024
1 parent 18b3376 commit a288686
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 132 deletions.
263 changes: 182 additions & 81 deletions ConvertFuncs.ahk

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion QuickConvertorV2.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ GuiTest(strV1Script:="")
SB.SetParts(300, 300, 300) ; Create four parts in the bar (the fourth part fills all the remaining width).

; Add folders and their subfolders to the tree. Display the status in case loading takes a long time:
M := Gui("ToolWindow -SysMenu Disabled AlwaysOnTop", "Loading the tree..."), M.Show("w200 h0")
M := Gui("ToolWindow -SysMenu Disabled AlwaysOnTop", "Loading the tree..."), M.Show("y100 w200 h0")

if TestFailing and TestMode{
DirList := AddSubFoldersToTree(A_ScriptDir "/tests", Map())
Expand Down
48 changes: 23 additions & 25 deletions convert/1Commands.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@
- use asterisk * and a function name to call, for custom processing when the params dont directly match up
*/

global gAhkCmdsToRemove := "
(
#AllowSameLineComments
#CommentFlag
#Delimiter
#DerefChar
#EscapeChar
#LTrim
#MaxMem
#NoEnv
SetBatchLines
SetFormat
SoundGetWaveVolume
SoundSetWaveVolume
SplashImage
A_FormatInteger
A_FormatFloat
AutoTrim
)"
global gmAhkCmdsToConvert := OrderedMap(
"BlockInput,OptionT2E" ,
"BlockInput({1})"
Expand Down Expand Up @@ -486,8 +506,8 @@ global gmAhkCmdsToConvert := OrderedMap(
, "#Warn,WarningType,WarningMode" ,
"*_HashtagWarn"
)

FindCommandDefinitions(Command, &v1:=unset, &v2:=unset) {
;################################################################################
FindCommandDefinitions(Command, &v1:=unset, &v2:=unset) {
for v1_, v2_ in gmAhkCmdsToConvert {
if (v1_ ~= "i)^\s*\Q" Command "\E\s*(,|$)") {
v1 := v1_
Expand All @@ -496,26 +516,4 @@ global gmAhkCmdsToConvert := OrderedMap(
}
}
return false
}

_SendMessage(p) {


if (p[3] ~= "^&.*"){
p[3] := SubStr(p[3],2)
Out := format('if (type(' . p[3] . ')="Buffer"){ `;V1toV2 If statement may be removed depending on type parameter`n`r' . gIndentation
. ' ErrorLevel := SendMessage({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})', p*)
Out := RegExReplace(Out, "[\s\,]*\)$", ")")
Out .= format('`n`r' . gIndentation . '} else{`n`r' . gIndentation
. ' ErrorLevel := SendMessage({1}, {2}, StrPtr({3}), {4}, {5}, {6}, {7}, {8}, {9})', p*)
Out := RegExReplace(Out, "[\s\,]*\)$", ")")
Out .= '`n`r' . gIndentation . "}"
return Out
}
if (p[3] ~= "^`".*") {
p[3] := 'StrPtr(' . p[3] . ')'
}

Out := format("ErrorLevel := SendMessage({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})", p*)
Return RegExReplace(Out, "[\s\,]*\)$", ")")
}
}
87 changes: 62 additions & 25 deletions convert/MaskCode.ahk
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
; 2026-06-26 ADDED by andymbody to support code block masking
; currently supports nested classes and functions (as wells as block/line comments and quoted strings for v1 or v2)
; supports nested classes and functions (as wells as block/line comments and quoted strings for v1 or v2)
; will add more support for other code blocks, AHK funcs, etc as needed
; all regex needles were designed to support masking tags. Feel free to contact me on AHK forum if I can assist with edits


; The FORMAT of this file was CREATED WITH TABS OF 4.
; Please do NOT change this to spaces or a different number of chars... Thanks!
; 2024-07-07 ADDED support for masking multi-line string blocks, removal of block/line comments, string blocks
; All regex needles were designed to support masking tags. Feel free to contact me on AHK forum if I can assist with edits


global gTagChar := chr(0x2605)
, gFuncPtn := buildPtn_FUNC(), gClassPtn := buildPtn_CLS()
, gLCPtn := '(*UCP)(?m)(?<=\s|);[^\v]*' ; line comment (allows lead ws to be consumed already)
, gFuncPtn := buildPtn_FUNC() ; function block (supports nesting)
, gClassPtn := buildPtn_CLS() ; class block (supports nesting)
, gMQSPtn := buildPtn_MStr() ; v1 multi-line string-block (non expression)
; 2024-07-07 AMB, CHANGED to bypass escaped semicolon
, gLCPtn := '(*UCP)(?m)(?<=\s|)(?<!``);[^\v]*' ; line comment (allows lead ws to be consumed already)
, gBCPtn := '(*UCP)(?m)^\h*(/\*((?>[^*/]+|\*[^/]|/[^*])*)(?>(?-2)(?-1))*(?:\*/|\Z))' ; block comments
, gQSPtn := '(*UCP)(?m)(?:`'`'|`'(?>[^`'\v]+(?:(?<=``)`')?)+`'|""|"(?>[^"\v]+(?:(?<=``)")?)+")' ; quoted string (UPDATED 2024-06-17)
, gMQSPtn := buildPtn_MStr() ; v1 multiline string (non expression)
; , gBracePtn := '(\{(?>[^}{]+|(?-1))*\})' ; nested brace blocks (for future support)

;################################################################################
Expand Down Expand Up @@ -149,7 +148,7 @@ class NodeMap
{
mCode := m[], doPreMask_remove(&mCode) ; remove premask of comments and strings
;node.taggedCode := mCode ; (not used)
node.ConvCode := _convertLines(mCode,finalize:=1) ; now convert code to v2
node.ConvCode := _convertLines(mCode,finalize:=1) ; now convert code to v2
code := RegExReplace(code, "\Q" mCode "\E", tag,,1,pos)
}
}
Expand Down Expand Up @@ -416,6 +415,7 @@ class MLSTR extends PreMask
; 2024-06-30 ADDED, AMB
; masks multiline strings
; MLSTR class is custom masking and convert class for multiline strings
; called from Before_LineConverts() of ConvertFuncs.ahk

doPreMask(&code) ; restore is handled in Convert() of ConvertFuncs.ahk
MLSTR.MaskAll(&code, 'MQS', gMQSPtn)
Expand All @@ -427,7 +427,7 @@ class MLSTR extends PreMask
{
; 2024-06-30 ADDED, AMB
; restore multiline strings
; called from Convert() of ConvertFuncs.ahk
; called from After_LineConverts() of ConvertFuncs.ahk

MLSTR.RestoreAll(&code, 'MQS') ; converts multiline string code as part of restore
return
Expand All @@ -440,6 +440,7 @@ class MLSTR extends PreMask
; 2024-06-02 UPDATED
; 2024-06-26 MOVED from ConvertFuncs.ahk. Just a proxy now
; masks quoted-strings

PreMask.MaskQS(&code)
return
}
Expand All @@ -450,22 +451,25 @@ class MLSTR extends PreMask
; 2024-04-08 ADDED, andymbody
; 2024-06-26 MOVED from ConvertFuncs.ahk. Just a proxy now
; restores orig strings that were masked by maskStrings()

PreMask.RestoreQS(&code)
return
}
;################################################################################
maskBlocks(&code)
;################################################################################
{
; proxy to mask classes and functions
; proxy func to mask classes and functions

NodeMap.MaskBlocks(&code)
return
}
;################################################################################
restoreBlocks(&code)
;################################################################################
{
; proxy to restore classes and functions
; proxy func to restore classes and functions

NodeMap.RestoreBlocks(&code)
return
}
Expand Down Expand Up @@ -495,14 +499,48 @@ class MLSTR extends PreMask
PreMask.RestoreBC(&code) ; restore block comments
return
}
;################################################################################
removeBCs(code)
;################################################################################
{
; 2024-07-07 AMB, ADDED - remove block comments from passed code

return RegExReplace(code,gBCPtn) ; remove block comments
}
;################################################################################
removeLCs(code)
;################################################################################
{
; 2024-07-07 AMB, ADDED - remove line comments from passed code

return RegExReplace(code,gLCPtn) ; remove line comments
}
;################################################################################
removeComments(code)
;################################################################################
{
; 2024-07-07 AMB, ADDED - remove block and line comments from passed code

code := removeBCs(code) ; remove block comments
return removeLCs(code) ; remove line comments
}
;################################################################################
removeMLStr(code)
;################################################################################
{
; 2024-07-07 AMB, ADDED - remove multi-line strings from passed code

return RegExReplace(code, gMQSPtn)
}
;################################################################################
buildPtn_CLS()
;################################################################################
{
; CLASS-BLOCK pattern of my own design
; CLASS-BLOCK pattern

; 2024-07-07 UPDATED comment needle to bypass escaped semicolon
opt := '(*UCP)(?im)' ; pattern options
LC := '(?:(?<=\s|);[^\v]*)' ; line comment (allows lead ws to be consumed already)
LC := '(?:(?<=\s|)(?<!``);[^\v]*)' ; line comment (allows lead ws to be consumed already)
tagChar := (IsSet(gTagChar)) ? gTagChar : chr(0x2605)
TG := '(?:#TAG' tagChar '\w+' tagChar '#)' ; mask tags
CT := '(?:' . LC . '|' . TG . ')*' ; optional line comment OR tag
Expand All @@ -519,16 +557,16 @@ class MLSTR extends PreMask
buildPtn_FUNC()
;################################################################################
{
; FUNCTION-BLOCK pattern of my own design
; supports class methods also (can begin with underscore)
; FUNCTION-BLOCK pattern - supports class methods also

; 2024-07-07 UPDATED comment needle to bypass escaped semicolon
opt := '(*UCP)(?im)' ; pattern options
LC := '(?:(?<=\s|);[^\v]*)' ; line comment (allows lead ws to be consumed already)
LC := '(?:(?<=\s|)(?<!``);[^\v]*)' ; line comment (allows lead ws to be consumed already)
tagChar := (IsSet(gTagChar)) ? gTagChar : chr(0x2605)
TG := '(?:#TAG' tagChar '\w+' tagChar '#)' ; mask tags
CT := '(?:' . LC . '|' . TG . ')*' ; optional line comment OR tag
TCT := '(?>\s*' . CT . ')*' ; optional trailing comment or tag (MUST BE ATOMIC)
exclude := '(?:\b(?:IF|WHILE|LOOP)\b)(?=\()\K|' ; \K| - cool trick I made to prevent If/While/Loop from being captured
exclude := '(?:\b(?:IF|WHILE|LOOP)\b)(?=\()\K|' ; \K| - added to prevent If/While/Loop from being captured
fName := '(?<fName>[_a-z]\w*)' ; fName - captures function/method name
fArgG := '(?<fArgG>\((?<Args>(?>[^()]|\((?&Args)\))*+)\))' ; fArgG - argument group (in parenth), Args - indv args (allows multiline span)
declare := fName . fArgG . TCT ; declare - function declaration
Expand All @@ -543,21 +581,20 @@ class MLSTR extends PreMask
{
; Multi-line string block
; non-expression version [ = ], not [ := ]
; does not support block comments between declaration and opening parenthesis
; does not currently support block comments between declaration and opening parenthesis
; can using masking to support them, or update needle to support raw block comments

; 2024-07-06, UPDATED for better performance
; 2024-07-07, UPDATED for better performance, updated comment needle to bypass escaped semicolon
opt := '(*UCP)(?ims)' ; pattern options
LC := '(?:(?<=\s);[^\v]*)' ; line comment (allows lead ws to be consumed already)
LC := '(?:(?<=\s)(?<!``);[^\v]*)' ; line comment (allows lead ws to be consumed already)
tagChar := (IsSet(gTagChar)) ? gTagChar : chr(0x2605)
TG := '(?:#TAG' tagChar '\w+' tagChar '#)' ; mask tags
CT := '(?<CT>(?:\s*+(?:' LC '|' TG '))*)' ; optional line comment OR tag
var := '(?<var>[_a-z]\w*)\h*=\h*' ; var - variable name
body := '\h*\R+(?<blk>\h*\((?<guts>(?:\R*(?>[^\v]*))*?)\R+\h*+\))' ; body - block body with parentheses and guts
pattern := opt . var . CT . body
; changed to line-at-a-time vs character-at-a-time -> 4-5 times faster, only fooled if original code syntax is incorrect
; (*UCP)(?ims)(?<var>[_a-z]\w*)\h*=\h*(?<CT>(?:\s*+(?:(?:(?<=\s);[^\v]*)|(?:#TAG★\w+★#)))*+)\h*\R+(?<blk>\h*\((?<guts>(?:\R*(?>[^\v]*))*?)\R+\h*+\))
; changed to line-at-a-time rather than char-at-a-time -> 4-5 times faster, only fooled if original code syntax is incorrect
; (*UCP)(?ims)(?<var>[_a-z]\w*)\h*=\h*(?<CT>(?:\s*+(?:(?:(?<=\s)(?<!``);[^\v]*)|(?:#TAG★\w+★#)))*+)\h*\R+(?<blk>\h*\((?<guts>(?:\R*(?>[^\v]*))*?)\R+\h*+\))
; A_Clipboard := pattern
; ExitApp
return pattern
}
40 changes: 40 additions & 0 deletions tests/Test_Folder/Environment/LabelNames_01.ah1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

; these are all VALID V1 label names
; V1 allows all character except whitespace, comma, and escape char.
; V2 allows ascii alphanumeric and underscore. But cannot start with a number. Does allow non-ascii characters.

; looks like a comment
`;notACommentAnymore:

; looks like variable de-reference path
%A_Desktop%\List.txt:

; looks like ahk directive
#Persistent:

; begins with digit and has dot
3.14:

; valid in v2
MYLABEL:

; non-ascii characters (Valid in v2)
★a★b★c:

; non-ascii characters (NOT valid due to digit prefix)
1★a★b★c:

; begins with colon FIXED
:a:

; looks like file path FIXED
c:\user\desktop\List.txt:

; looks like regex needle FIXED
\w+\(.*?\):

; looks like an if statement FIXED
if(a&&b){:

; looks like a function FIXED
IThoughtIWasAFunc(param1:=""){:
40 changes: 40 additions & 0 deletions tests/Test_Folder/Environment/LabelNames_01.ah2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

; these are all VALID V1 label names
; V1 allows all character except whitespace, comma, and escape char.
; V2 allows ascii alphanumeric and underscore. But cannot start with a number. Does allow non-ascii characters.

; looks like a comment
__notACommentAnymore:

; looks like variable de-reference path
_A_Desktop__List_txt:

; looks like ahk directive
_Persistent:

; begins with digit and has dot
_3_14:

; valid in v2
MYLABEL:

; non-ascii characters (Valid in v2)
★a★b★c:

; non-ascii characters (NOT valid due to digit prefix)
_1★a★b★c:

; begins with colon FIXED
_a:

; looks like file path FIXED
c__user_desktop_List_txt:

; looks like regex needle FIXED
_w________:

; looks like an if statement FIXED
if_a__b__:

; looks like a function FIXED
IThoughtIWasAFunc_param1______:
13 changes: 13 additions & 0 deletions tests/Test_Folder/Environment/LabelNames_02.ah1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; Part of issue #201
#Requires AutoHotkey v1.1.33+
; show foo bar

gosub 1)(miro#_r

MsgBox, % "bar"
Return

; label
1)(miro#_r:
MsgBox, % "foo"
Return
16 changes: 16 additions & 0 deletions tests/Test_Folder/Environment/LabelNames_02.ah2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; Part of issue #201
#Requires Autohotkey v2.0
; show foo bar

_1__miro__r()

MsgBox("bar")
Return

; label
_1__miro__r()
{ ; V1toV2: Added bracket
global ; V1toV2: Made function global
MsgBox("foo")
Return
} ; V1toV2: Added bracket in the end

0 comments on commit a288686

Please sign in to comment.