Пример #1
0
// Check some number of children, all using the same rules.
// The given max and min counts are inclusive. Pass -1 for no maximum limit.
static bool check_children(ast_t* ast, check_state_t* state,
  const check_fn_t *rules, size_t min_count, size_t max_count, errors_t* errors)
{
  assert(ast != NULL);
  assert(state != NULL);
  assert(min_count <= max_count);

  size_t found_count = 0;

  while(found_count < max_count && state->child != NULL)
  {
    // See if next child is suitable
    check_res_t r = check_from_list(state->child, rules, errors);

    if(r == CHK_ERROR)  // Propogate error
      return false;

    if(r == CHK_NOT_FOUND)  // End of list
      break;

    // Child found
    state->child = ast_sibling(state->child);
    state->child_index++;
    found_count++;
  }

  // There are no more matching children
  if(found_count >= min_count)
    return true;

  // Didn't find enough matching children
  if(state->child == NULL)
  {
    error_preamble(ast);
    printf("found " __zu " child%s, expected more\n", state->child_index,
      (state->child_index == 1) ? "" : "ren");
    ast_error(state->errors, ast, "Here");
    ast_print(ast);
#ifdef IMMEDIATE_FAIL
    assert(false);
#endif
  }
  else
  {
    error_preamble(ast);
    printf("child " __zu " has invalid id %d\n", state->child_index,
      ast_id(state->child));
    ast_error(state->errors, ast, "Here");
    ast_print(ast);
#ifdef IMMEDIATE_FAIL
    assert(false);
#endif
  }

  return false;
}
Пример #2
0
// Check some number of children, all using the same rules.
// The given max and min counts are inclusive. Pass -1 for no maximum limit.
static bool check_children(ast_t* ast, check_state_t* state,
  const check_fn_t *rules, size_t min_count, size_t max_count)
{
  assert(ast != NULL);
  assert(state != NULL);
  assert(min_count <= max_count);

  size_t found_count = 0;

  while(found_count < max_count && state->child != NULL)
  {
    // See if next child is suitable
    check_res_t r = check_from_list(state->child, rules);

    if(r == CHK_ERROR)  // Propogate error
      return false;

    if(r == CHK_NOT_FOUND)  // End of list
      break;

    // Child found
    state->child = ast_sibling(state->child);
    state->child_index++;
    found_count++;
  }

  // There are no more matching children
  if(found_count >= min_count)
    return true;

  // Didn't find enough matching children
  if(state->child == NULL)
  {
    printf("Internal error: AST node id %d has %ld child%s, expected more\n",
      ast_id(ast), state->child_index, (state->child_index == 1) ? "" : "ren");
  }
  else
  {
    printf("Internal error: AST node id %d, child %ld has invalid id %d\n",
      ast_id(ast), state->child_index, ast_id(state->child));
  }

  return false;
}