hmmer-3.3/easel/esl_json.c
/* Function:  esl_json_Validate()
 * Synopsis:  Validate a JSON parse tree structure
 * Incept:    SRE, Tue 31 Jul 2018 [Clint Mansell, Moon soundtrack]
 *
 * Purpose:   Validate internals of JSON parse tree <pi>. If optional
 *            <bf> is provided, do additional validation that
 *            substrings of the parsed input appear to match what the
 *            parse tree says they should be. If all seems ok, return
 *            <eslOK>. If bad, return <eslFAIL> and (if optional
 *            <errbuf> is provided), put an informative user-directed
 *            error message in <errbuf>.
 *
 * Args:      pi     - parse tree to validate
 *            bf     - optional - input buffer that <pi> corresponds to, or NULL
 *            errbuf - optional - informative error message on failure,  or NULL
 *
 * Returns:   <eslOK> on success. <errbuf>, if it was provided, is an empty string.
 *
 *            <eslFAIL> on failure. <errbuf>, if it was provided, contains 
 *            an informative error message.
 */
esl_json_Validate()
Synopsis:  Validate a JSON parse tree structure
Incept:    SRE, Tue 31 Jul 2018 [Clint Mansell, Moon soundtrack]

Validate internals of JSON parse tree . If optional
 is provided, do additional validation that
substrings of the parsed input appear to match what the
parse tree says they should be. If all seems ok, return
. If bad, return  and (if optional
 is provided), put an informative user-directed
error message in .



 on success. , if it was provided, is an empty string.

 on failure. , if it was provided, contains
an informative error message.
esl_json_Validate () Synopsis : Validate a JSON parse tree structure Incept : SRE Tue 31 Jul 2018 [ Clint Mansell Moon soundtrack ] Validate internals of JSON parse tree . If optional is provided do additional validation that substrings of the parsed input appear to match what the parse tree says they should be . If all seems ok return . If bad return and ( if optional is provided ) put an informative user - directed error message in . on success . if it was provided is an empty string . on failure . if it was provided contains an informative error message .
int
esl_json_Validate(const ESL_JSON *pi, const ESL_BUFFER *bf, char *errbuf)
{
  int i,n;
  ESL_JSON_TOK *tok;
  int       cur,  prv;   
  esl_pos_t pos1, pos2;  
  if (errbuf) errbuf[0] = '\0';
  for (i = 0; i < pi->ntok; i++)
    {
      tok  = &(pi->tok[i]);
      pos1 = (bf ? tok->startpos - bf->baseoffset : tok->startpos);  
      pos2 = (bf ? tok->endpos   - bf->baseoffset : tok->endpos);
      if (pos1 < 0) ESL_FAIL(eslFAIL, errbuf, "bad start pos, tok %d", i);
      if (pos2 < 0) ESL_FAIL(eslFAIL, errbuf, "bad end pos, tok %d",   i);
      if ((tok->type == eslJSON_KEY || tok->type == eslJSON_STRING)) 
	{ if (pos2 < pos1-1) ESL_FAIL(eslFAIL, errbuf, "bad end pos, string/key tok %d",   i); } 
      else
	{ if (pos2 < pos1) ESL_FAIL(eslFAIL, errbuf, "bad end pos, tok %d",   i); }              
      for (cur = tok->firstchild, n=0, prv=-1; cur != -1; cur = pi->tok[cur].nextsib) { n++; prv = cur; }
      if (tok->nchild > 0 &&  (tok->firstchild == -1 || tok->lastchild == -1))    ESL_FAIL(eslFAIL, errbuf, "bad child links, tok %d", i);
      if (tok->nchild == 0 && (tok->firstchild != -1 || tok->lastchild != -1))    ESL_FAIL(eslFAIL, errbuf, "tok %d shouldn't have child links");
      if (tok->nchild    != n)                                                    ESL_FAIL(eslFAIL, errbuf, "bad number of children, tok %d",   i);
      if (tok->lastchild != prv)                                                  ESL_FAIL(eslFAIL, errbuf, "bad child linked list for tok %d", i);
      if (bf)
	{
	  if (pos1 >= bf->n) ESL_FAIL(eslFAIL, errbuf, "bad start pos, tok %d", i);
	  if (pos2 >= bf->n) ESL_FAIL(eslFAIL, errbuf, "bad end pos, tok %d",   i);
	  switch (tok->type) {
	  case eslJSON_OBJECT:  if (bf->mem[pos1]   != '{' || bf->mem[pos2]   != '}')        ESL_FAIL(eslFAIL, errbuf, "object closing brackets missing, tok %d", i); break;
	  case eslJSON_ARRAY:   if (bf->mem[pos1]   != '[' || bf->mem[pos2]   != ']')        ESL_FAIL(eslFAIL, errbuf, "array closing brackets missing, tok %d",  i); break;
	  case eslJSON_KEY:     if (bf->mem[pos1-1] != '"' || bf->mem[pos2+1] != '"')        ESL_FAIL(eslFAIL, errbuf, "key quotes missing, tok %d",              i); break;
	  case eslJSON_STRING:  if (bf->mem[pos1-1] != '"' || bf->mem[pos2+1] != '"')        ESL_FAIL(eslFAIL, errbuf, "string quotes missing, tok %d",           i); break;
	  case eslJSON_NUMBER:  if (! esl_mem_IsReal(bf->mem + pos1, pos2-pos1+1))           ESL_FAIL(eslFAIL, errbuf, "number isn't a number, tok %d",           i); break;
	  case eslJSON_BOOLEAN: if (! esl_memstrcmp(bf->mem + pos1, pos2-pos1+1, "true")  &&
				    ! esl_memstrcmp(bf->mem + pos1, pos2-pos1+1, "false"))   ESL_FAIL(eslFAIL, errbuf, "boolean isn't a boolean, tok %d",         i); break;
	  case eslJSON_NULL:    if (! esl_memstrcmp(bf->mem + pos1, pos2-pos1+1, "null"))    ESL_FAIL(eslFAIL, errbuf, "null isn't null, tok %d",                 i); break;
	  default: ESL_FAIL(eslFAIL, errbuf, "no such state type %d, tok %d", (int) tok->type, i);
          }
	}
    }
  return eslOK;
}
int esl_json_Validate ( const ESL_JSON * pi , const ESL_BUFFER * bf , char * errbuf ) { int i , n ; ESL_JSON_TOK * tok ; int cur , prv ; esl_pos_t pos1 , pos2 ; if ( errbuf ) errbuf [ 0 ] = ' \0 ' ; for ( i = 0 ; i < pi -> ntok ; i ++ ) { tok = & ( pi -> tok [ i ] ) ; pos1 = ( bf ? tok -> startpos - bf -> baseoffset : tok -> startpos ) ; pos2 = ( bf ? tok -> endpos - bf -> baseoffset : tok -> endpos ) ; if ( pos1 < 0 ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( pos2 < 0 ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( ( tok -> type == eslJSON_KEY || tok -> type == eslJSON_STRING ) ) { if ( pos2 < pos1 - 1 ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; } else { if ( pos2 < pos1 ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; } for ( cur = tok -> firstchild , n = 0 , prv = -1 ; cur != -1 ; cur = pi -> tok [ cur ] . nextsib ) { n ++ ; prv = cur ; } if ( tok -> nchild > 0 && ( tok -> firstchild == -1 || tok -> lastchild == -1 ) ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( tok -> nchild == 0 && ( tok -> firstchild != -1 || tok -> lastchild != -1 ) ) ESL_FAIL ( eslFAIL , errbuf , " " ) ; if ( tok -> nchild != n ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( tok -> lastchild != prv ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( bf ) { if ( pos1 >= bf -> n ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; if ( pos2 >= bf -> n ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; switch ( tok -> type ) { case eslJSON_OBJECT : if ( bf -> mem [ pos1 ] != ' ' || bf -> mem [ pos2 ] != ' ' ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_ARRAY : if ( bf -> mem [ pos1 ] != ' ' || bf -> mem [ pos2 ] != ' ' ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_KEY : if ( bf -> mem [ pos1 - 1 ] != ' ' || bf -> mem [ pos2 + 1 ] != ' ' ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_STRING : if ( bf -> mem [ pos1 - 1 ] != ' ' || bf -> mem [ pos2 + 1 ] != ' ' ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_NUMBER : if ( ! esl_mem_IsReal ( bf -> mem + pos1 , pos2 - pos1 + 1 ) ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_BOOLEAN : if ( ! esl_memstrcmp ( bf -> mem + pos1 , pos2 - pos1 + 1 , " " ) && ! esl_memstrcmp ( bf -> mem + pos1 , pos2 - pos1 + 1 , " " ) ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; case eslJSON_NULL : if ( ! esl_memstrcmp ( bf -> mem + pos1 , pos2 - pos1 + 1 , " " ) ) ESL_FAIL ( eslFAIL , errbuf , " " , i ) ; break ; default : ESL_FAIL ( eslFAIL , errbuf , " " , ( int ) tok -> type , i ) ; } } } return eslOK ; }
Function:  esl_json_Validate()
 Synopsis:  Validate a JSON parse tree structure
 Incept:    SRE, Tue 31 Jul 2018 [Clint Mansell, Moon soundtrack]
Function : esl_json_Validate () Synopsis : Validate a JSON parse tree structure Incept : SRE Tue 31 Jul 2018 [ Clint Mansell Moon soundtrack ]
// token indices, following linked list of children // start, end coords for a token, adjusted to bf->mem coords if <bf> is passed // bf->mem[0] = s[baseoffset] in original input coords for <s> // a zero-length string or key has endpos = startpos-1 /* integrity of child linked list  */ /* optionally, if <bf> provided, partially validate each substring */