aat

git mirror of https://ccx.te2000.cz/bzr/aat
git clone https://ccx.te2000.cz/git/aat
Log | Files | Refs | README

commit 02f55b81110d1dcf21acfefcb41136037227a363
parent dafa82ec9d25596ea28471a5ab0494cc334883e5
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date:   Tue, 17 Sep 2013 01:15:18 +0200

make hello3.aat work
Diffstat:
Mbin/aat.awk | 4+---
Mbin/aat_macros.sed | 2+-
Mdata | 6++++++
Mdata.awk | 38+++++++++++++++++++++++++++++++-------
Mhello3.aat | 12+++++++++---
5 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/bin/aat.awk b/bin/aat.awk @@ -36,7 +36,6 @@ function call_macro(name, args) { # Macro to insert another file as verbatim code else if(name == "awk"){ - filename=substr(line, 6) tok_type=T_AWK while(getline <args) { token($0 "\n") @@ -45,7 +44,6 @@ function call_macro(name, args) { # Macro to insert another file as text else if(name == "text"){ - filename=substr(line, 7) while(getline <args) { token($0 "\n") } @@ -54,7 +52,7 @@ function call_macro(name, args) { # Leave the @ there for postprocessing with sed else { tok_type=T_AWK - token("@" line "\n") + token("@" name " " args "\n") } } diff --git a/bin/aat_macros.sed b/bin/aat_macros.sed @@ -6,7 +6,7 @@ s/^[[:space:]]*ELIF[[:space:]]\+\(.\+\)$/} else if(\1) {\n/ s/^[[:space:]]*ELSE[[:space:]]*$/} else {/ s/^[[:space:]]*ENDIF[[:space:]]*$/}/ -s/^[[:space:]]*@for[[:space:]]\+\([[:alnum:]]\+\)[[:space:]]\+in[[:space:]]\+\(.\+\)$/for(_loop_\1=loop_start("\2", "\1_"); loop_iter(_loop_\1);) {/ +s/^[[:space:]]*@for[[:space:]]\+\([[:alnum:]]\+\)[[:space:]]\+in[[:space:]]\+\(.\+\)$/for(_loop_\1=loop_start("\2", "\1."); loop_iter(_loop_\1);) {/ s/^[[:space:]]*@endfor[[:space:]]*$/} loop_end()/ s/^[[:space:]]*@if[[:space:]]\+\(.\+\)$/if(\1) {\n/ s/^[[:space:]]*@elif[[:space:]]\+\(.\+\)$/} else if(\1) {\n/ diff --git a/data b/data @@ -4,3 +4,9 @@ name value eggs hello world 12 fish thanks 42 ] +names=[ +name +Jack +Joe +Jonathan +] diff --git a/data.awk b/data.awk @@ -1,7 +1,7 @@ #!/bin/awk -f BEGIN { IFS="\t" - SUBSEP=":" + SUBSEP="." state = 0 # 0 read normal variables # 1 seen start of field list, read titles @@ -17,13 +17,12 @@ $0 == "]" && state == 2 { } state == 2 { - V[varname,list_n++] = $0 + V[varname,++V[varname,"rows"]] = $0 next } state == 1 { V[varname,"fields"] = $0 - list_n = 1 state = 2 next } @@ -75,16 +74,41 @@ function loop_end() { loop_stack["depth"]-- } -function get(varname, i, n, names, values) { +function get(varname, i, n, names, values, loopvar, looprow, prefix) { for(i=loop_stack["depth"]; i>0; i--) { - split(V[loop_stack[i, "var"], "fields"], names) + prefix = loop_stack[i, "pre"] + if(prefix) + if(substr(varname, 1, length(prefix)) != prefix) + continue + loopvar = loop_stack[i, "var"] + looprow = loop_stack[i, "row"] + if(varname == prefix"_index") + return looprow + if(varname == prefix"_index0") + return looprow - 1 + if(varname == prefix"_revindex") + return V[loopvar, "rows"] - looprow + 1 + if(varname == prefix"_revindex0") + return V[loopvar, "rows"] - looprow + if(varname == prefix"_first") + return looprow == 1 + if(varname == prefix"_last") + return looprow == V[loopvar, "rows"] + if(varname == prefix"_length") + return V[loopvar, "rows"] + split(V[loopvar, "fields"], names) for(n in names) { - if(varname == (loop_stack[i, "pre"] names[n])) { - split(V[loop_stack[i, "var"], loop_stack[i, "row"]], values) + if(varname == (prefix names[n])) { + split(V[loopvar, looprow], values) return values[n] } } } + if(DEBUG) { + if(!(varname in V)) { + print "variable not found: " varname >"/dev/stderr" + } + } return V[varname] } diff --git a/hello3.aat b/hello3.aat @@ -1,9 +1,15 @@ -|!/bin/awk -f @awk data.awk |END { -@ for name in names -Hello {{get("name_v")}}{{get("name__last") ? "!" : ","}} +@for x in spam +{{get("x.name")}} - {{get("x.value")}} +@ for y in spam +{{ get("x.eggs") + get("y.eggs") }} @ endfor +@endfor +-------------------- +@for n in names +Hello {{get("n.name") (get("n._last") ? "!" : ",")}} +@endfor Welcome to the world of {{toupper("awk")}} templating! |}