Example #1
0
static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
  char* tmpbuf;
  int len;
  if (!tok) {
    append_buf(buf, "NULL", 4);
    return;
  }
  switch (tok->token_type) {
  case TT_NONE:
    append_buf(buf, "null", 4);
    break;
  case TT_BYTES:
    if (tok->bytes.len == 0)
      append_buf(buf, "<>", 2);
    else {
      for (size_t i = 0; i < tok->bytes.len; i++) {
	const char *HEX = "0123456789abcdef";
	append_buf_c(buf, (i == 0) ? '<': '.');
	char c = tok->bytes.token[i];
	append_buf_c(buf, HEX[(c >> 4) & 0xf]);
	append_buf_c(buf, HEX[(c >> 0) & 0xf]);
      }
      append_buf_c(buf, '>');
    }
    break;
  case TT_SINT:
    if (tok->sint < 0)
      len = asprintf(&tmpbuf, "s-%#lx", -tok->sint);
    else
      len = asprintf(&tmpbuf, "s%#lx", tok->sint);
    append_buf(buf, tmpbuf, len);
    free(tmpbuf);
    break;
  case TT_UINT:
    len = asprintf(&tmpbuf, "u%#lx", tok->uint);
    append_buf(buf, tmpbuf, len);
    free(tmpbuf);
    break;
  case TT_ERR:
    append_buf(buf, "ERR", 3);
    break;
  case TT_SEQUENCE: {
    append_buf_c(buf, '(');
    for (size_t i = 0; i < tok->seq->used; i++) {
      if (i > 0)
	append_buf_c(buf, ' ');
      unamb_sub(tok->seq->elements[i], buf);
    }
    append_buf_c(buf, ')');
  }
    break;
  default:
    fprintf(stderr, "Unexpected token type %d\n", tok->token_type);
    assert_message(0, "Should not reach here.");
  }
}
Example #2
0
char* h_write_result_unamb__m(HAllocator* mm__, const HParsedToken* tok) {
  struct result_buf buf = {
    .output = mm__->alloc(mm__, 16),
    .len = 0,
    .mm__ = mm__,
    .capacity = 16
  };
  unamb_sub(tok, &buf);
  append_buf_c(&buf, 0);
  return buf.output;
}
Example #3
0
char* h_write_result_unamb(const HParsedToken* tok) {
  struct result_buf buf = {
    .output = (&system_allocator)->alloc(&system_allocator, 16),
    .len = 0,
    .capacity = 16
  };
  if (!buf.output) {
    return NULL;
  }
  unamb_sub(tok, &buf);
  append_buf_c(&buf, 0);
  return buf.output;
}