Skip to content

Commit

Permalink
Improve parsing algorithm to handle spaces and comments #63
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Oct 10, 2021
1 parent 0c3fcfa commit 6f6d1bf
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BEGIN {

{
Lines[NR]=$0
if ($1 ~ /^@/) reparseCli()
if ("@options" == $1) handleOptions()
else if ("@define" == $1) handleDefine()
else if ("@shell" == $1) handleShell()
Expand Down Expand Up @@ -695,6 +696,43 @@ function join(arr, startIncl, endExcl, sep, result, i) {
result = result sep arr[i]
return result
}
function parseCli(line, res, pos,c,last) {
for(pos=1;;) {
while((c = substr(line,pos,1))==" " || c == "\t") pos++ # consume spaces
if ((c = substr(line,pos,1))=="#" || c=="")
return
else if (c == "'") { # start of string
# consume quoted string
res[last = res[-7]++] = ""
while((c = substr(line,++pos,1)) != "'") { # closing '
if (c=="")
return "unterminated argument"
else if (c=="\\" && substr(line,pos+1,1)=="'") { # escaped '
c = "'"; pos++
}
res[last] = res[last] c
}
if((c = substr(line,++pos,1)) != "" && c != " " && c != "\t")
return "joined arguments"
} else {
# consume unquoted argument
res[last = res[-7]++] = c
while((c = substr(line,++pos,1)) != "" && c != " " && c != "\t") { # whitespace denotes end of arg
if(c=="'")
return "joined arguments"
res[last] = res[last] c
}
}
}
}
function reparseCli( res,i) {
err = parseCli($0, res)
if (err)
die("syntax error at line " NR ": " err)
else
for (i=NF=0; i in res; i++)
$(++NF)=res[i]
}
function addStr(target, str) { target[0] = target[0] str }
function addLine(target, line) { target[0] = addL(target[0], line) }
function addL(s, l) { return s ? s "\n" l : l }
Expand Down

0 comments on commit 6f6d1bf

Please sign in to comment.