예제 #1
0
파일: parser.c 프로젝트: Tao-Ma/flatcc
static void parse_value(fb_parser_t *P, fb_value_t *v, int flags, const char *error_msg)
{
    fb_token_t *t;
    fb_token_t *sign;

    sign = optional(P, '-');
    t = P->token;

    switch (t->id) {
    case LEX_TOK_INT:
        read_integer_value(P, t, v, sign != 0);
        break;
    case LEX_TOK_FLOAT:
        read_float_value(P, t, v, sign != 0);
        break;
    case tok_kw_true:
        v->b = 1;
        v->type = vt_bool;
        break;
    case tok_kw_false:
        v->b = 0;
        v->type = vt_bool;
        break;
    case LEX_TOK_STRING_BEGIN:
        next(P);
        parse_string_literal(P, v);
        if (!(flags & allow_string_value)) {
            v->type = vt_invalid;
            error_tok(P, t, error_msg);
            return;
        }
        if (sign) {
            v->type = vt_invalid;
            error_tok(P, t, "string constants cannot be signed");
            return;
        }
        return;
    case LEX_TOK_ID:
        parse_ref(P, &v->ref);
        v->type = vt_name_ref;
        if (sign) {
            v->type = vt_invalid;
            /* Technically they could, but we do not allow it. */
            error_tok(P, t, "named values cannot be signed");
        }
        return;
    default:
        /* We might have consumed a sign, but never mind that. */
        error_tok(P, t, error_msg);
        return;
    }
    if (sign && v->type == vt_bool) {
        v->type = vt_invalid;
        error_tok(P, t, "boolean constants cannot be signed");
    }
    next(P);
}
예제 #2
0
bool lsb_read_heka_field(lsb_heka_message *m,
                         lsb_const_string *name,
                         int fi,
                         int ai,
                         lsb_read_value *val)
{
  if (!m || !name || !val) {
    return false;
  }

  int fcnt = 0;
  const char *p, *e;
  val->type = LSB_READ_NIL;

  for (int i = 0; i < m->fields_len; ++i) {
    if (name->len == m->fields[i].name.len
        && strncmp(name->s, m->fields[i].name.s, m->fields[i].name.len) == 0) {
      if (fi == fcnt++) {
        p = m->fields[i].value.s;
        e = p + m->fields[i].value.len;
        switch (m->fields[i].value_type) {
        case LSB_PB_STRING:
        case LSB_PB_BYTES:
          return read_string_value(p, e, ai, val);
        case LSB_PB_INTEGER:
          return read_integer_value(p, e, ai, val);
        case LSB_PB_BOOL:
          if (read_integer_value(p, e, ai, val)) {
            val->type = LSB_READ_BOOL;
            return true;
          }
          return false;
        case LSB_PB_DOUBLE:
          return read_double_value(p, e, ai, val);
        default:
          return false;
        }
      }
    }
  }
  return false;
}