From e29a77e491af52312471b319b160d331c371d52d Mon Sep 17 00:00:00 2001 From: xonix Date: Wed, 20 Dec 2023 19:44:11 +0200 Subject: [PATCH] auto-format using the latest intellij-awk : adjust minify.awk --- makesure_candidate | 10 +++++----- minify.awk | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/makesure_candidate b/makesure_candidate index 5c517f5..d1e9686 100755 --- a/makesure_candidate +++ b/makesure_candidate @@ -256,7 +256,7 @@ function checkBeforeRun(i,j,dep,depCnt,goalName){ if(!((dep=Dependencies[goalName,j])in GoalsByName)) 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])}} + addError("Goal " quote2(goalName,1)" uses unknown lib '\''" GoalToLib[goalName]"'\''",UseLibLineNo[goalName])}} function getPreludeCode(a,k){ addLine(a,MyDirScript) for(k in Vars) @@ -345,7 +345,7 @@ function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j die("Goal not found: " goalName) topologicalSortPerform(includeReachedIf,reachedGoals,goalName,result,loop)} if(loop[0]==1) - die("There is a loop in goal dependencies via " loop[1] " -> " loop[2])} + 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")} @@ -354,7 +354,7 @@ 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" @@ -654,6 +654,6 @@ 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 trim(s){sub(/^[ \t\r\n]+/,"", s);sub(/[ \t\r\n]+$/, "",s);return s} +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 "$@" diff --git a/minify.awk b/minify.awk index 0e9a5ac..c658578 100644 --- a/minify.awk +++ b/minify.awk @@ -32,27 +32,40 @@ in_begin && $1 ~ /^delete/{ next } gsub(/} +/, "}") gsub(/[)] +/, ")") gsubKeepStrings("] +", "]") - gsub(/print +"/,"print\"") - gsub(/printf +"/,"printf\"") + gsub(/print +"/, "print\"") + gsub(/printf +"/, "printf\"") if (!/^ +}/) gsub(/ +}/, "}") ##gsub(/" in/, "\"in") gsub(Q, Q "\\" Q Q) if (l = trim($0)) { decreaseIndent(); printf "%s", (l == "}" ? l : (NR == 1 ? "" : "\n") $0) } } function decreaseIndent() { - match($0,/^ */) - $0 = sprintf("%" (RLENGTH-1)/2 "s","") substr($0,RLENGTH) + match($0, /^ */) + $0 = sprintf("%" (RLENGTH - 1) / 2 "s", "") substr($0, RLENGTH) } -function gsubKeepStrings(regex, replacement, cnt,parts,i,s) { - if ((cnt = split($0,parts,"\"")) == 1) { - gsub(regex,replacement) - return +function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) { + nonString = "" + s = "" + isString = 0 + for (i=1;(c=substr($0,i,1))!="";i++) { +# print "> " i " " c + if ("\"" == c) { + # TODO escaped " + if (!isString) { + gsub(regex, replacement, nonString) + s = s nonString + nonString = "" + } + s = s c # append this " + isString = !isString + } else if (isString) { + s = s c + } else { + nonString = nonString c + } } - gsub(regex,replacement,parts[1]) - gsub(regex,replacement,parts[cnt]) - for (i = 1; i < cnt; i++) - s = s parts[i] "\"" - s = s parts[cnt] + gsub(regex, replacement, nonString) + s = s nonString $0 = s } -#BEGIN { $0 = "aaa\"aaa\"bbb"; gsubKeepStrings("aaa","AAA"); print } \ No newline at end of file +#BEGIN { $0 = "aaa\"aaa\"bbbaaa"; gsubKeepStrings("aaa","AAA"); print } \ No newline at end of file