Skip to content

Commit

Permalink
auto-format using the latest intellij-awk : adjust minify.awk
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Dec 20, 2023
1 parent 5c879a3 commit c6a2324
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
108 changes: 54 additions & 54 deletions makesure_candidate
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,30 @@ function handleOptions(i){
addError("Provide at least one option")
for(i=2;i<=NF;i++){
if(!($i in SupportedOptions))
addError("Option '\''" $i "'\'' is not supported")
addError("Option '\''" $i"'\'' is not supported")
Options[$i]}}
function handleDefine(){
started("define")
if(NF!=3){
addError("Invalid @define syntax, should be @define VAR_NAME '\''value'\''")
return}
if($2 !~ /^[A-Za-z_][A-Za-z0-9_]*$/){
addError("Wrong variable name: '\''" $2 "'\''")
addError("Wrong variable name: '\''" $2"'\''")
return}
if(!($2 in DefineOverrides))
Vars[$2]=$3}
function handleShell(){
checkPreludeOnly()
if(!((Shell=$2)in SupportedShells))
addError("Shell '\''" Shell "'\'' is not supported")}
addError("Shell '\''" Shell"'\'' is not supported")}
function timingOn(){
return "timing"in Options&&!("silent"in Options)}
return"timing"in Options&&!("silent"in Options)}
function started(mode){
Mode=mode}
function handleLib(libName){
started("lib")
if((libName=$2)in Lib){
addError("Lib '\''" libName "'\'' is already defined")}
addError("Lib '\''" libName"'\'' is already defined")}
arrPush(LibNames,libName)
Lib[libName]}
function handleUseLib(i){
Expand All @@ -143,7 +143,7 @@ function handleGoal(i,goalName){
GoalParams[goalName,GoalParamsCnt[goalName]++]=validateParamName($i)
}else if(NF>2)addError("nothing allowed after goal name")}
function validateParamName(p){
if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''" p "'\''")
if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''" p"'\''")
return p}
function registerGoal(priv,goalName){
if(""==goalName||"@params"==goalName)
Expand All @@ -154,14 +154,14 @@ function registerGoal(priv,goalName){
arrPush(GoalNames,goalName)
GoalsByName[goalName]=priv
return 1}}
function globGoal(i){return (GlobGoalName?GlobGoalName "@":"")GlobFiles[i]}
function globGoal(i){return (GlobGoalName?GlobGoalName"@":"")GlobFiles[i]}
function calcGlob(goalName,pattern,script,file){
GlobCnt=0
GlobGoalName=goalName
delete GlobFiles
script=MyDirScript ";for f in " pattern ";do test -e \"$f\"&&echo \"$f\"||:;done"
script=MyDirScript";for f in " pattern";do test -e \"$f\"&&echo \"$f\"||:;done"
if("sh"!=Shell)
script=Shell " -c " quoteArg(script)
script=Shell" -c " quoteArg(script)
while((script|getline file)>0){
GlobCnt++
arrPush(GlobFiles,file)}
Expand Down Expand Up @@ -204,7 +204,7 @@ function handleDoc(i){
registerDoc(globGoal(i))}}
function registerDoc(goalName){
if(goalName in Doc)
addError("Multiple " $1 " not allowed for a goal")
addError("Multiple " $1" not allowed for a goal")
$1=""
Doc[goalName]=trim($0)}
function handleDependsOn(i){
Expand Down Expand Up @@ -241,10 +241,10 @@ function handleReachedIf(i){
for(i=0;i<GlobCnt;i++)
registerReachedIf(globGoal(i),makeGlobVarsCode(i))}
function makeGlobVarsCode(i){
return "ITEM=" quoteArg(GlobFiles[i])";INDEX=" i ";TOTAL=" GlobCnt ";"}
return"ITEM=" quoteArg(GlobFiles[i])";INDEX=" i";TOTAL=" GlobCnt";"}
function registerReachedIf(goalName,preScript){
if(goalName in ReachedIf)
addError("Multiple " $1 " not allowed for a goal")
addError("Multiple " $1" not allowed for a goal")
trimDirective()
ReachedIf[goalName]=preScript trim($0)}
function trimDirective(){
Expand All @@ -254,13 +254,13 @@ function checkBeforeRun(i,j,dep,depCnt,goalName){
depCnt=DependenciesCnt[goalName=GoalNames[i]]
for(j=0;j<depCnt;j++)
if(!((dep=Dependencies[goalName,j])in GoalsByName))
addError("Goal " quote2(goalName,1)" has unknown dependency '\''" dep "'\''",DependenciesLineNo[goalName,j])
addError("Goal " quote2(goalName,1)" has unknown dependency '\''" dep"'\''",DependenciesLineNo[goalName,j])
if(goalName in GoalToLib&&!(GoalToLib[goalName]in Lib))
addError("Goal " quote2(goalName,1)" uses unknown lib '\''" GoalToLib[goalName]"'\''",UseLibLineNo[goalName])}}
function getPreludeCode(a,k){
addLine(a,MyDirScript)
for(k in Vars)
addLine(a,k "=" quoteArg(Vars[k])";export " k)
addLine(a,k"=" quoteArg(Vars[k])";export " k)
return a[0]}
function doWork(\
i,goalName,gnLen,gnMaxLen,reachedGoals,emptyGoals,preludeCode,
Expand All @@ -286,7 +286,7 @@ body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
continue
printf" "
if(goalName in Doc)
printf"%-" gnMaxLen "s : %s\n",quote2(goalName),Doc[goalName]
printf"%-" gnMaxLen"s : %s\n",quote2(goalName),Doc[goalName]
else
print quote2(goalName)}
}else {
Expand Down Expand Up @@ -346,15 +346,15 @@ function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j
topologicalSortPerform(includeReachedIf,reachedGoals,goalName,result,loop)}
if(loop[0]==1)
die("There is a loop in goal dependencies via " loop[1]" -> " loop[2])}
function isCodeAllowed(){return "goal"==Mode||"goal_glob"==Mode||"lib"==Mode}
function isPrelude(){return "prelude"==Mode}
function checkPreludeOnly(){if(!isPrelude())addError("Only use " $1 " in prelude")}
function checkGoalOnly(){if("goal"!=Mode&&"goal_glob"!=Mode)addError("Only use " $1 " in @goal")}
function isCodeAllowed(){return"goal"==Mode||"goal_glob"==Mode||"lib"==Mode}
function isPrelude(){return"prelude"==Mode}
function checkPreludeOnly(){if(!isPrelude())addError("Only use " $1" in prelude")}
function checkGoalOnly(){if("goal"!=Mode&&"goal_glob"!=Mode)addError("Only use " $1" in @goal")}
function currentGoalName(){return arrLast(GoalNames)}
function currentLibName(){return arrLast(LibNames)}
function realExit(code){
exit code}
function addError(err,n){if(!n)n=NR;Error=addL(Error,err ":\n" ARGV[1]":" n ": " Lines[n])}
function addError(err,n){if(!n)n=NR;Error=addL(Error,err":\n" ARGV[1]":" n": " Lines[n])}
function addErrorDedup(err,n){if((err,n)in AddedErrors)return;AddedErrors[err,n];addError(err,n)}
function die(msg,out){
out="cat 1>&2"
Expand All @@ -364,15 +364,15 @@ function die(msg,out){
function checkConditionReached(goalName,conditionStr,script){
script=getPreludeCode()
if(goalName in GoalToLib)
script=script "\n" Lib[GoalToLib[goalName]]
script=script "\n" conditionStr
return shellExec(script,goalName "@reached_if")==0}
script=script"\n" Lib[GoalToLib[goalName]]
script=script"\n" conditionStr
return shellExec(script,goalName"@reached_if")==0}
function shellExec(script,comment,res){
if("tracing"in Options){
script=": " quoteArg(comment)"\n" script
script=Shell " -x -e -c " quoteArg(script)
script=Shell" -x -e -c " quoteArg(script)
}else
script=Shell " -e -c " quoteArg(script)
script=Shell" -e -c " quoteArg(script)
script="trap '\''exit 7'\'' INT;" script
res=system(script)
return res}
Expand Down Expand Up @@ -443,15 +443,15 @@ function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstant
copyKey(goal,goalNameInstantiated,ReachedIf)
copyKey(goal,goalNameInstantiated,GoalToLib)
for(i in args)
argsCode=addL(argsCode,i "=" quoteArg(args[i]))
argsCode=addL(argsCode,i"=" quoteArg(args[i]))
CodePre[goalNameInstantiated]=addL(CodePre[goalNameInstantiated],argsCode)
if(goalNameInstantiated in ReachedIf)
ReachedIf[goalNameInstantiated]=argsCode "\n" ReachedIf[goalNameInstantiated]}
ReachedIf[goalNameInstantiated]=argsCode"\n" ReachedIf[goalNameInstantiated]}
for(i=0;i<DependenciesCnt[goal];i++){
dep=Dependencies[gi=goal SUBSEP i]
argsCnt=+DependencyArgsCnt[gi]
if(dep in GoalsByName&&argsCnt!=GoalParamsCnt[dep])
addErrorDedup("wrong args count for '\''" dep "'\''",DependenciesLineNo[gi])
addErrorDedup("wrong args count for '\''" dep"'\''",DependenciesLineNo[gi])
for(j=0;j<argsCnt;j++){
depArg=DependencyArgs[gi,j]
depArgType=DependencyArgsType[gi,j]
Expand All @@ -460,7 +460,7 @@ function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstant
depArg : \
depArgType=="var"?\
(depArg in args?args[depArg]:\
depArg in Vars?Vars[depArg]:addErrorDedup("wrong arg '\''" depArg "'\''",DependenciesLineNo[gi])):\
depArg in Vars?Vars[depArg]:addErrorDedup("wrong arg '\''" depArg"'\''",DependenciesLineNo[gi])):\
die("wrong depArgType: " depArgType)}
gii=goalNameInstantiated SUBSEP i
Dependencies[gii]=instantiate(dep,newArgs)
Expand All @@ -471,7 +471,7 @@ function instantiateGoalName(goal,args,res,cnt,i){
if((cnt=GoalParamsCnt[goal])==0)return goal
res=goal
for(i=0;i<cnt;i++)
res=res "@" args[GoalParams[goal,i]]
res=res"@" args[GoalParams[goal,i]]
return res}
function currentTimeMillis(res){
if(Gawk)
Expand All @@ -485,12 +485,12 @@ function selfUpdate(url,tmp,err,newVer){
err=dl(url,tmp)
if(!err&&!ok("chmod +x " tmp))err="can'\''t chmod +x " tmp
if(!err){
newVer=executeGetLine(tmp " -v")
newVer=executeGetLine(tmp" -v")
if(Version!=newVer){
if(!ok("cp " tmp " " quoteArg(Prog)))
if(!ok("cp " tmp" " quoteArg(Prog)))
err="can'\''t overwrite " Prog
else print"updated " Version " -> " newVer
}else print"you have latest version " Version " installed"}
else print"updated " Version" -> " newVer
}else print"you have latest version " Version" installed"}
rm(tmp)
if(err)die(err)}
function renderDuration(deltaMillis,\
Expand All @@ -508,17 +508,17 @@ function renderDuration(deltaMillis,\
if(deltaHr>=24){
deltaDay=int(deltaHr/24)
deltaHr=deltaHr-deltaDay*24}
dayS=deltaDay>0?deltaDay " d":""
hrS=deltaHr>0?deltaHr " h":""
minS=deltaMin>0?deltaMin " m":""
secS=deltaSec>0?deltaSec " s":""
dayS=deltaDay>0?deltaDay" d":""
hrS=deltaHr>0?deltaHr" h":""
minS=deltaMin>0?deltaMin" m":""
secS=deltaSec>0?deltaSec" s":""
secSI=deltaSec>0?int(deltaSec)" s":""
if(dayS!="")
res=dayS " " (hrS==""?"0 h":hrS)
res=dayS" " (hrS==""?"0 h":hrS)
else if(deltaHr>0)
res=hrS " " (minS==""?"0 m":minS)
res=hrS" " (minS==""?"0 m":minS)
else if(deltaMin>0)
res=minS " " (secSI==""?"0 s":secSI)
res=minS" " (secSI==""?"0 s":secSI)
else
res=deltaSec>0?secS:"0 s"
return res}
Expand All @@ -531,11 +531,11 @@ function dl(url,dest,verbose){
verbose="VERBOSE"in ENVIRON
if(commandExists("wget")){
if(!ok("wget " (verbose?"":"-q")" " quoteArg(url)" -O" quoteArg(dest)))
return "error with wget"
return"error with wget"
}else if(commandExists("curl")){
if(!ok("curl " (verbose?"":"-s")" " quoteArg(url)" -o " quoteArg(dest)))
return "error with curl"
}else return "wget/curl not found"}
return"error with curl"
}else return"wget/curl not found"}
function natOrder(s1,s2,i1,i2,c1,c2,n1,n2){
if(_digit(c1=substr(s1,i1,1))&&_digit(c2=substr(s2,i2,1))){
n1=+c1;while(_digit(c1=substr(s1,++i1,1))){n1=n1*10+c1}
Expand Down Expand Up @@ -574,7 +574,7 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
w=""
while((c=substr(line,++pos,1))!=q){
if(c=="")
return "unterminated argument"
return"unterminated argument"
else if(c=="\\"&&((c1=substr(line,pos+1,1))=="'\''"&&isDoll||c1==c||q=="\""&&(c1==q||c1=="$"))){
c=c1;pos++
}else if(c=="$"&&q=="\""){
Expand All @@ -584,7 +584,7 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
if((c1=substr(line,pos+1,1))=="{"){
for(pos++;(c=substr(line,++pos,1))!="}";){
if(c=="")
return "unterminated argument"
return"unterminated argument"
if(!inDef&&":"==c&&"-"==substr(line,pos+1,1)){
inDef=1
c=substr(line,pos += 2,1)}
Expand All @@ -600,20 +600,20 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
for(;(c=substr(line,pos+1,1))~/[_A-Za-z0-9]/;pos++)
var=var c
if(var !~ /^[_A-Za-z][_A-Za-z0-9]*$/)
return "wrong var: '\''" var "'\''"
return"wrong var: '\''" var"'\''"
w=(w)((val=var in vars?vars[var]:ENVIRON[var])!=""?val:defVal)
continue}
w=w c}
res[i=+res[-7]++,"quote"]=isDoll?"$":q
res[i]=w
if((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t")
return "joined arguments"
return"joined arguments"
}else {
w=c
while((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t"){
w=w c}
if(w !~ /^[_A-Za-z0-9@.]+$/)
return "wrong unquoted: '\''" w "'\''"
return"wrong unquoted: '\''" w"'\''"
res[i=+res[-7]++,"quote"]="u"
res[i]=w}}}}
function reparseCli(res,i,err){
Expand All @@ -634,11 +634,11 @@ function quote2(s,force){
if(index(s,"'\''")){
gsub(/\\/,"\\\\",s)
gsub(/'\''/,"\\'\''",s)
return "$'\''" s "'\''"
return"$'\''" s"'\''"
}else
return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''" s "'\''":s}
return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''" s"'\''":s}
function addLine(target,line){target[0]=addL(target[0],line)}
function addL(s,l){return s?s "\n" l:l}
function addL(s,l){return s?s"\n" l:l}
function arrPush(arr,elt){arr[arr[-7]++]=elt}
function arrLen(arr){return +arr[-7]}
function arrDel(arr,v,l,i,e,resArr){
Expand All @@ -650,10 +650,10 @@ function arrDel(arr,v,l,i,e,resArr){
for(i in resArr)
arr[i]=resArr[i]}
function arrLast(arr,l){return (l=arrLen(arr))>0?arr[l-1]:""}
function commandExists(cmd){return ok("command -v " cmd " >/dev/null")}
function commandExists(cmd){return ok("command -v " cmd" >/dev/null")}
function ok(cmd){return system(cmd)==0}
function isFile(path){return ok("test -f " quoteArg(path))}
function rm(f){system("rm " quoteArg(f))}
function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return "'\''" a "'\''"}
function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return"'\''" a"'\''"}
function trim(s){sub(/^[ \t\r\n]+/,"",s);sub(/[ \t\r\n]+$/,"",s);return s}
function copyKey(keySrc,keyDst,arr){if(keySrc in arr)arr[keyDst]=arr[keySrc]}' Makesurefile "$@"
7 changes: 4 additions & 3 deletions minify.awk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ in_begin && $1 ~ /^delete/{ next }
gsub(/printf +"/, "printf\"")
if (!/^ +}/) gsub(/ +}/, "}")
gsubKeepStrings("^ +in ", "in ")
gsubKeepStrings(" +$", "")
gsub(Q, Q "\\" Q Q)
if (l = trim($0)) { decreaseIndent(); printf "%s", (l == "}" ? l : (NR == 1 ? "" : "\n") $0) }
}
Expand All @@ -49,8 +50,7 @@ function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) {
isString = 0
for (i=1;(c=substr($0,i,1))!="";i++) {
# print "> " i " " c
if ("\"" == c) {
# TODO escaped "
if ("\"" == c && (substr($0,i-1,1) != "\\" || substr($0,i-2,1) == "\\")) {
if (!isString) {
gsub(regex, replacement, nonString)
s = s nonString
Expand All @@ -69,4 +69,5 @@ function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) {
$0 = s
}
#BEGIN { $0 = "aaa\"aaa\"bbbaaa"; gsubKeepStrings("aaa","AAA"); print }
#BEGIN { $0 = "if (\"-h\" in Args || \"--help\" in Args) {"; gsubKeepStrings("^ +in", "in"); print }
#BEGIN { $0 = "aaa\"aaa\\\"aaa\"bbbaaa"; gsubKeepStrings("aaa","AAA"); print }
#BEGIN { $0 = "if (\"-h\" in Args || \"--help\" in Args) {"; gsubKeepStrings("^ +in", "in"); print }

0 comments on commit c6a2324

Please sign in to comment.