From fb58415024698a92d20f15ff399b810f4e9bb033 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 16 Mar 2020 09:34:06 -0400 Subject: [PATCH] Add AWK hxpipe parser lib --- home/lib/parse_hxpipe.awk | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 home/lib/parse_hxpipe.awk diff --git a/home/lib/parse_hxpipe.awk b/home/lib/parse_hxpipe.awk new file mode 100644 index 0000000..ab07779 --- /dev/null +++ b/home/lib/parse_hxpipe.awk @@ -0,0 +1,62 @@ +# API: +# XmlPath : string +# XmlAttr : dict : [XmlPath, string] -> string +# XmlPayload : string + +/^[\(\)]/ { + update_node() + next +} + +/^A/ && $2 == "CDATA" { + update_node_attributes() + next +} + +/^-/ { + XmlPayload = substr($0, 2, length($0)) +} + +function path_to_string(path, depth, p, i) { + p = "" + for (i = 1; i <= depth; i++) { + p = p "/" path[i] + } + return p +} + +function update_node( paren, name, key, val, path, attr) { + paren = substr($1, 1, 1) + name = substr($1, 2, length($1) - 1) + if (paren == "(") { + _depth++ + _path[_depth] = name + XmlPath = path_to_string(_path, _depth) + for (key in _hxpipe_curr_attrs) { + val = _hxpipe_curr_attrs[key] + XmlAttr[XmlPath, key] = val + } + } else if (paren == ")") { + delete _hxpipe_curr_attrs + XmlPayload = "" + for (key in XmlAttr) { + split(key, k, SUBSEP) + path = k[1] + attr = k[2] + if (path == XmlPath) delete XmlAttr[key] + } + _depth-- + XmlPath = path_to_string(_path, _depth) + } else { + printf("ERROR in input line %d - not a parenthesis: \"%s\"\n", NR, paren) > "/dev/stderr" + exit 1 + } +} + +function update_node_attributes( key, val, s) { + key = substr($1, 2, length($1)) + val = $0 + s = " +" + sub("^" $1 s $2 s, "", val) + _hxpipe_curr_attrs[key] = val +} -- 2.20.1