commit d91687ac4f961958555aedd804a7673f7f2ed6bb
parent a274a2d22b5ea1244ba5e08a4d3bc1c2ae670138
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Wed, 9 Oct 2013 18:48:47 +0200
filters, query-aware macros
Diffstat:
4 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/bin/aat.awk b/bin/aat.awk
@@ -45,11 +45,35 @@ function call_macro(name, args) {
# Macro to insert another file as text
else if(name == "text"){
+ tok_type=T_TEXT
while(getline <args) {
token($0 "\n")
}
}
+ # Assign a variable with query expression
+ else if(name == "let"){
+ if (!match(args, / *= */)) {
+ print "ERROR: invalid let statement: " args >"/dev/stderr"
+ exit 1
+ }
+ tok_type=T_AWK
+ token("V[\"" substr(args, 1, RSTART-1) "\"] = " \
+ aat_process(substr(args, RSTART+RLENGTH)) "\n")
+ }
+
+ # if with a query expression
+ else if(name == "if"){
+ tok_type=T_AWK
+ token("if(" aat_process(substr(args, RSTART+RLENGTH)) ") {\n")
+ }
+
+ # else if with a query expression
+ else if(name == "elif"){
+ tok_type=T_AWK
+ token("} else if(" aat_process(substr(args, RSTART+RLENGTH)) ") {\n")
+ }
+
# Leave the @ there for postprocessing with sed
else {
tok_type=T_AWK
@@ -251,7 +275,6 @@ function aat_process(str) {
}
}
}
- # TODO filters
while(str) {
if(!match(str, /^\|[a-zA-Z_][a-zA-Z0-9_.]*/)) {
print "ERROR: invalid filter: " str >"/dev/stderr"
@@ -274,7 +297,14 @@ function aat_process(str) {
args = args substr(str, 1, RLENGTH)
str = substr(str, RLENGTH+1)
}
- # TODO
+ } else if(match(str, /^ /)) {
+ if(match(str, /\|[a-zA-Z_]/)) {
+ args = ", " substr(str, 1, RLENGTH-1)
+ str = substr(str, RSTART)
+ } else {
+ args = ", " str
+ str = ""
+ }
}
processed = filter_name "(" processed args ")"
}
diff --git a/bin/aat_macros.sed b/bin/aat_macros.sed
@@ -9,7 +9,8 @@ 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:]]\+var[[: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/
+s/^[[:space:]]*@if[[:space:]]\+\(.\+\)$/if(get("\1")) {\n/
+s/^[[:space:]]*@elif[[:space:]]\+\(.\+\)$/} else if(get("\1")) {\n/
s/^[[:space:]]*@else[[:space:]]*$/} else {/
s/^[[:space:]]*@endif[[:space:]]*$/}/
+s/^[[:space:]]*@let[[:space:]]\+\([a-zA-Z_][a-zA-Z0-9_]*\)[[:space:]]*=[[:space:]]*\(.*\)$/V["\1"] = \2\n/
diff --git a/hello4.aat b/hello4.aat
@@ -0,0 +1,15 @@
+@awk data.awk
+|END {
+@for x in spam
+{<x.name>} - {<x.value>}
+@ for y in spam
+{{ get("x.eggs") + get("y.eggs") }}
+@ endfor
+@endfor
+--------------------
+@for n in names
+Hello {<n.name>}{{get("n._last") ? "!" : ","}}
+@endfor
+
+Welcome to the world of {{toupper("awk")}} templating!
+|}
diff --git a/hello5.aat b/hello5.aat
@@ -0,0 +1,16 @@
+@awk data.awk
+|END {
+@for x in spam
+{<x.name>} - {<x.value>}
+@ for y in spam
+{< x.eggs + y.eggs >}
+@ endfor
+@endfor
+--------------------
+@for n in names
+Hello {<n.name>}{<n._last ? "!" : ",">}
+@endfor
+
+@let language = "awk"
+Welcome to the world of {<language|toupper>} templating!
+|}