020b692014-10-23Arne Goedeke /* vim:syntax=ragel */
a9deeb2010-06-10Arne Goedeke  %%{ machine JSON_mapping; alphtype int; include JSOND "json_defaults.rl";
a9e2ef2010-06-10Arne Goedeke  getkey ((int)INDEX_PCHARP(str, fpc));
a9deeb2010-06-10Arne Goedeke  action parse_value {
b56cb92010-06-10Arne Goedeke  state->level++; p = _parse_JSON(str, fpc, pe, state); state->level--; if (state->flags&JSON_ERROR) {
020b692014-10-23Arne Goedeke  if (validate) {
019d282010-08-20Henrik Grubbström (Grubba)  pop_2_elems(); /* pop mapping and key */
b56cb92010-06-10Arne Goedeke  } return p;
020b692014-10-23Arne Goedeke  } else if (validate) {
b56cb92010-06-10Arne Goedeke  mapping_insert(m, &(Pike_sp[-2]), &(Pike_sp[-1])); pop_2_elems(); } c++; fexec p;
a9deeb2010-06-10Arne Goedeke  } action parse_key {
b56cb92010-06-10Arne Goedeke  state->level++; if (state->flags&JSON_UTF8) p = _parse_JSON_string_utf8(str, fpc, pe, state); else p = _parse_JSON_string(str, fpc, pe, state); state->level--; if (state->flags&JSON_ERROR) {
020b692014-10-23Arne Goedeke  if (validate) {
019d282010-08-20Henrik Grubbström (Grubba)  pop_stack(); /* pop mapping */
b56cb92010-06-10Arne Goedeke  } return p; } c++; fexec p;
a9deeb2010-06-10Arne Goedeke  } main := '{' . myspace* . ( start: ( '}' -> final | '"' >parse_key . myspace* . ':' -> value ), value: ( myspace* . value_start >parse_value . myspace* -> repeat ), repeat: ( ',' . myspace* -> start | '}' -> final ) ) %*{ fpc--; fbreak; }; }%%
a9e2ef2010-06-10Arne Goedeke static ptrdiff_t _parse_JSON_mapping(PCHARP str, ptrdiff_t p, ptrdiff_t pe, struct parser_state *state) {
25ebad2014-10-23Arne Goedeke  /* GCC complains about a being used uninitialized. This is clearly wrong, so * lets silence this warning */ struct mapping *m = m;
a9deeb2010-06-10Arne Goedeke  int cs; int c = 0;
020b692014-10-23Arne Goedeke  const int validate = !(state->flags&JSON_VALIDATE);
a9deeb2010-06-10Arne Goedeke  %% write data;
b56cb92010-06-10Arne Goedeke  /* Check stacks since we have uncontrolled recursion here. */ check_stack (10); check_c_stack (1024);
020b692014-10-23Arne Goedeke  if (validate) {
b56cb92010-06-10Arne Goedeke  m = debug_allocate_mapping(5); push_mapping(m);
a9deeb2010-06-10Arne Goedeke  } %% write init; %% write exec; if (cs >= JSON_mapping_first_final) {
b56cb92010-06-10Arne Goedeke  return p;
a9deeb2010-06-10Arne Goedeke  }
a9e2ef2010-06-10Arne Goedeke  state->flags |= JSON_ERROR;
020b692014-10-23Arne Goedeke  if (validate) {
019d282010-08-20Henrik Grubbström (Grubba)  if (c & 1) pop_2_elems(); /* pop key and mapping */
b56cb92010-06-10Arne Goedeke  else pop_stack();
a9deeb2010-06-10Arne Goedeke  }
a9e2ef2010-06-10Arne Goedeke  return p;
a9deeb2010-06-10Arne Goedeke }