WiP pass YamlTreeParser where needed

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:02 +01:00
parent d1c7ae4141
commit b7640a830f
28 changed files with 424 additions and 411 deletions
+76 -81
View File
@@ -49,24 +49,25 @@ uint32_t yaml_parse_enum(const struct YamlIdStr* choices, const char* val, uint8
return choices->id;
}
static void yaml_set_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
uint16_t idx, const char* val, uint8_t val_len)
static void yaml_set_attr(void* user, uint8_t* ptr, uint32_t bit_ofs,
const YamlNode* node, const char* val,
uint8_t val_len)
{
uint32_t i = 0;
uint32_t i = 0;
//TRACE("set(%s, %.*s, bit-ofs=%u, bits=%u)\n",
// node->tag, val_len, val, bit_ofs, node->size);
// TRACE("set(%s, %.*s, bit-ofs=%u, bits=%u)\n",
// node->tag, val_len, val, bit_ofs, node->size);
ptr += bit_ofs >> 3UL;
bit_ofs &= 0x07;
ptr += bit_ofs >> 3UL;
bit_ofs &= 0x07;
if (node->type == YDT_STRING) {
//assert(!bit_ofs);
copy_string((char*)ptr, val, MIN(val_len, node->size - 1));
return;
}
if (node->type == YDT_STRING) {
// assert(!bit_ofs);
copy_string((char*)ptr, val, MIN(val_len, node->size - 1));
return;
}
switch(node->type) {
switch (node->type) {
case YDT_SIGNED:
i = node->u._cust.cust_to_uint ? node->u._cust.cust_to_uint(node, val, val_len)
: (uint32_t)yaml_str2int(val, val_len);
@@ -80,7 +81,7 @@ static void yaml_set_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
break;
case YDT_CUSTOM:
if (node->u._cust_attr.read)
node->u._cust_attr.read(node, ptr, bit_ofs, idx, val, val_len);
node->u._cust_attr.read(user, ptr, bit_ofs, val, val_len);
return;
default:
break;
@@ -130,71 +131,64 @@ static bool yaml_output_string(const char* str, uint32_t max_len,
return true;
}
static bool yaml_output_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
uint16_t idx, yaml_writer_func wf, void* opaque)
static bool yaml_output_attr(void* user, uint8_t* ptr, uint32_t bit_ofs,
const YamlNode* node, yaml_writer_func wf,
void* opaque)
{
if (node->type == YDT_NONE)
if (node->type == YDT_NONE) return false;
if (node->type == YDT_PADDING) return true;
// output tag
if (!wf(opaque, node->tag, node->tag_len)) return false;
if (!wf(opaque, ": ", 2)) return false;
if (ptr) {
ptr += bit_ofs >> 3UL;
bit_ofs &= 0x07;
const char* p_out = NULL;
if (node->type == YDT_STRING) {
// assert(!bit_ofs);
if (!yaml_output_string((const char*)ptr, (node->size) >> 3UL, wf,
opaque))
return false;
if (node->type == YDT_PADDING)
return true;
// output tag
if (!wf(opaque, node->tag, node->tag_len))
} else if (node->type == YDT_CUSTOM) {
if (node->u._cust_attr.write &&
!node->u._cust_attr.write(user, ptr, bit_ofs, wf, opaque))
return false;
} else {
unsigned int i = yaml_get_bits(ptr, bit_ofs, node->size);
if (!wf(opaque, ": ", 2))
return false;
if ((node->type == YDT_SIGNED || node->type == YDT_UNSIGNED) &&
node->u._cust.uint_to_cust) {
return node->u._cust.uint_to_cust(node, i, wf, opaque) &&
wf(opaque, "\r\n", 2);
} else {
switch (node->type) {
case YDT_SIGNED:
p_out = yaml_signed2str((int)yaml_to_signed(i, node->size));
break;
case YDT_UNSIGNED:
p_out = yaml_unsigned2str(i);
break;
case YDT_ENUM:
p_out = yaml_output_enum(i, node->u._enum.choices);
break;
if (ptr) {
ptr += bit_ofs >> 3UL;
bit_ofs &= 0x07;
const char* p_out = NULL;
if (node->type == YDT_STRING) {
//assert(!bit_ofs);
if (!yaml_output_string((const char*)ptr, (node->size)>>3UL, wf, opaque))
return false;
case YDT_ARRAY:
case YDT_UNION:
default:
break;
}
else if (node->type == YDT_CUSTOM) {
if (node->u._cust_attr.write
&& !node->u._cust_attr.write(node, ptr, bit_ofs, idx, wf, opaque))
return false;
}
else {
unsigned int i = yaml_get_bits(ptr, bit_ofs, node->size);
if ((node->type == YDT_SIGNED || node->type == YDT_UNSIGNED)
&& node->u._cust.uint_to_cust) {
return node->u._cust.uint_to_cust(node, i, wf, opaque)
&& wf(opaque, "\r\n", 2);
}
else {
switch(node->type) {
case YDT_SIGNED:
p_out = yaml_signed2str((int)yaml_to_signed(i, node->size));
break;
case YDT_UNSIGNED:
p_out = yaml_unsigned2str(i);
break;
case YDT_ENUM:
p_out = yaml_output_enum(i, node->u._enum.choices);
break;
case YDT_ARRAY:
case YDT_UNION:
default:
break;
}
}
}
if (p_out && !wf(opaque, p_out, strlen(p_out)))
return false;
}
}
return wf(opaque, "\r\n", 2);
if (p_out && !wf(opaque, p_out, strlen(p_out))) return false;
}
return wf(opaque, "\r\n", 2);
}
YamlTreeWalker::YamlTreeWalker()
@@ -389,7 +383,7 @@ bool YamlTreeWalker::isElmtEmpty(uint8_t* data)
// assume structs aligned on 8bit boundaries
if (node->u._array.u._a.is_active)
return !node->u._array.u._a.is_active(data, bit_ofs, getElmts());
return !node->u._array.u._a.is_active(this, data, bit_ofs);
return yaml_is_zero(data, bit_ofs, node->size);
}
@@ -452,7 +446,7 @@ void YamlTreeWalker::setAttrValue(char* buf, uint8_t len)
uint32_t i = 0;
if (attr->u._cust_idx.read)
i = attr->u._cust_idx.read(buf, len);
i = attr->u._cust_idx.read(this, buf, len);
else
i = yaml_str2uint(buf, len);
@@ -465,7 +459,7 @@ void YamlTreeWalker::setAttrValue(char* buf, uint8_t len)
}
}
else {
yaml_set_attr(data, getBitOffset(), attr, getParentElmts(), buf, len);
yaml_set_attr(this, data, getBitOffset(), attr, buf, len);
}
}
@@ -533,19 +527,20 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
for(int i=2; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(NULL, 0, node, 0, wf, opaque))
if (!yaml_output_attr(this, NULL, 0, node, wf, opaque))
return false; // TODO: error handling???
// grab attr idx...
uint8_t idx = node->u._array.u.select_member(data, getBitOffset());
//TRACE("<idx = %d>", idx);
uint8_t idx =
node->u._array.u.select_member(this, data, getBitOffset());
// TRACE("<idx = %d>", idx);
setAttrIdx(idx);
attr = getAttr();
for(int i=1; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(data, getBitOffset(), attr, 0, wf, opaque))
if (!yaml_output_attr(this, data, getBitOffset(), attr, wf, opaque))
return false; // TODO: error handling???
if (attr->type != YDT_ARRAY
@@ -578,7 +573,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
for(int i=2; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(NULL, 0, getNode(),0, wf, opaque))
if (!yaml_output_attr(this, NULL, 0, getNode(), wf, opaque))
return false; // TODO: error handling???
continue;
}
@@ -600,7 +595,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
return false;
if (attr->u._cust_idx.write) {
if (!attr->u._cust_idx.write(getElmts(),wf,opaque))
if (!attr->u._cust_idx.write(this,wf,opaque))
return false;
}
else {
@@ -633,7 +628,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(data, getBitOffset(), attr, getParentElmts(), wf, opaque))
if (!yaml_output_attr(this, data, getBitOffset(), attr, wf, opaque))
return false; // TODO: error handling???
}