=== modified file 'data.awk' --- data.awk 2013-09-16 16:47:56 +0000 +++ data.awk 2013-09-11 23:26:09 +0000 @@ -6,8 +6,7 @@ # 0 read normal variables # 1 seen start of field list, read titles # 2 reading list values - # ident_re = /[a-zA-Z_][a-zA-Z0-9_]*/ - ident_re = "[a-zA-Z_][a-zA-Z0-9_]*" + ident_re = /[a-zA-Z_][a-zA-Z0-9_]*/ } $0 == "]" && state == 2 { @@ -24,81 +23,32 @@ state == 1 { V[varname,"fields"] = $0 list_n = 1 - state = 2 next } -/^[a-zA-Z_][a-zA-Z0-9_]*=\[$/ { +/[a-zA-Z_][a-zA-Z0-9_]*=\[$/ { # start of field list state = 1 - m = match($0, ident_re) + m = match(ident_re) varname = substr($0, m, RLENGTH) next } -/^[a-zA-Z_][a-zA-Z0-9_]*=/ { - m = match($0, ident_re) +/[a-zA-Z_][a-zA-Z0-9_]*=/ { + m = match(ident_re) varname = substr($0, m, RLENGTH) V[varname] = substr($0, m+1+RLENGTH) - printf "got scalar: %s ⇒ %s (%d)\n", varname, V[varname], m next } -/^\t/ { +/\t/ { V[varname] = V[varname] "\n" substr($0, 1) - next -} - -{ - # TODO posix compliance - print "unparseable line: " $0 >"/dev/stderr" - exit 1 -} - -function loop_start(varname, prefix, depth) { - depth = ++loop_stack["depth"] - loop_stack[depth,"row"] = 0 - loop_stack[depth,"var"] = varname - loop_stack[depth,"pre"] = prefix - return depth -} - -function loop_iter(depth) { - loop_stack[depth,"row"]++ - return (loop_stack[depth,"var"], loop_stack[depth,"row"]) in V -} - -function loop_end() { - delete loop_stack[loop_stack["depth"], "row"] - delete loop_stack[loop_stack["depth"], "var"] - delete loop_stack[loop_stack["depth"], "pre"] - loop_stack["depth"]-- -} - -function get(varname, i, n, names, values) { - for(i=loop_stack["depth"]; i>0; i--) { - split(V[loop_stack[i, "var"], "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) - return values[n] - } - } - } - return V[varname] -} - -# just testing it out +} + +# TODO error + END { for(key in V) { - printf("%s⇒\t→%s←\n", key, V[key]) + printf("s\ts\n", key, V[key]) } - print "--------------------" - print get("foo") - for(d1=loop_start("spam"); loop_iter(d1);) { - print get("name") "-" get("value") - for(d2=loop_start("spam", "i_"); loop_iter(d2);) { - print get("eggs") + get("i_eggs") - } - } loop_end() }