aat

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

commit 97d8e58aa0b3d1c1783ce9bf33db80d7ed084cd7
parent 8cf49cea854596c120e44b3f04664543b093abae
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date:   Tue,  8 Oct 2013 20:28:24 +0200

WIP function-parsed expressions
Diffstat:
Mbin/aat.awk | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 59 insertions(+), 2 deletions(-)

diff --git a/bin/aat.awk b/bin/aat.awk @@ -6,6 +6,7 @@ BEGIN { T_TEXT = 1; type_names[1] = "text" T_EXPR = 2; type_names[2] = "expr" T_AWK = 3; type_names[3] = "awk" + T_FUNC = 4; type_names[4] = "func" tok_type = T_TEXT } @@ -90,19 +91,35 @@ function parse_line(line) { eat_nl = 0 if(tok_type == T_TEXT) { # all text until a start of expression "{{", or start of awk code "{%" - m = match(line, /\{[{%]/) + m = match(line, /\{[{%<]/) if(m) { token(substr(line, 1, m-1)) if (substr(line, m, RLENGTH) == "{{") tok_type = T_EXPR - else + else if (substr(line, m, RLENGTH) == "{%") tok_type = T_AWK + else if (substr(line, m, RLENGTH) == "{<") + tok_type = T_FUNC + else { print "internal error" >"/dev/stderr"; exit 1 } line = substr(line, m+RLENGTH) } else { # no delimiter found, whole line is text token(line) line = "" } + } else if(tok_type == T_FUNC) { + m = match(line, />}/) + if(m) { + if(DEBUG) printf "expr match: \"%s\"\n", substr(line, m, RLENGTH) >"/dev/stderr" + token(substr(line, 1, RSTART-1)) + tok_finished[tok_n] = 1 + line = substr(line, RSTART+RLENGTH) + tok_type = T_TEXT + } else { + # did not match whole expression because of end of line + token(line) + line = "" + } } else if(tok_type == T_EXPR || tok_type == T_AWK) { # match text inside awk code or expression # code stops on "%}" and expression on "}}" @@ -154,6 +171,43 @@ function parse_line(line) { } } +# TODO make this pluggable +# called for expanding {< >} +function expand_query( expanded) { + expanded = "" + while(match(expand_remaining, "['<>]")) { + if(RSTART > 1) + expanded = expanded "\"" substr(expand_remaining, 1, RSTART-1) "\"" + char = substr(expand_remaining, RSTART, 1) + expand_remaining = substr(expand_remaining, RSTART+1) + if(char == ">") + return "get("expanded")" + else if(char == "<") { + expanded = expanded expand_query() + } else if(char == "'") { + if(match(expand_remaining, "'")) { + expanded = expanded substr(expand_remaining, 1, RSTART-1) + expand_remaining = substr(expand_remaining, RSTART+1) + } else { + expanded = expand_remaining + expand_remaining = "" + } + } else { + print "ERROR: internal error in expand_query()" >"/dev/stderr" + exit 1 + } + } + if(expand_remaining) { + expanded = "\"" expand_remaining "\"" + expand_remaining = "" + } + return "get("expanded")" +} +function aat_process(str) { + expand_remaining = str + return expand_query() +} + { parse_line($0) } END { @@ -177,6 +231,9 @@ END { nl = 1 } else if(tok_type == T_EXPR) { printf " (%s)", c + } else if(tok_type == T_FUNC) { + # TODO + printf " (%s)", aat_process(c) } else { print "ERROR: unknown tok_type: " tok_type >"/dev/stderr" exit 1