Skip to content
Snippets Groups Projects
xor.c 1.41 KiB
Newer Older
Dan Hirsch's avatar
Dan Hirsch committed
#include "parser_internal.h"

typedef struct {
  const HParser *p1;
  const HParser *p2;
} HTwoParsers;


static HParseResult* parse_xor(void *env, HParseState *state) {
  HTwoParsers *parsers = (HTwoParsers*)env;
  // cache the initial state of the input stream
  HInputStream start_state = state->input_stream;
  HParseResult *r1 = h_do_parse(parsers->p1, state);
  HInputStream after_p1_state = state->input_stream;
  // reset input stream, parse again
  state->input_stream = start_state;
  HParseResult *r2 = h_do_parse(parsers->p2, state);
  if (NULL == r1) {
    if (NULL != r2) {
      return r2;
    } else {
      return NULL;
    }
  } else {
    if (NULL == r2) {
      state->input_stream = after_p1_state;
      return r1;
    } else {
      return NULL;
    }
  }
}

static bool xor_isValidCF(void *env) {
  HTwoParsers *tp = (HTwoParsers*)env;
  return (tp->p1->vtable->isValidCF(tp->p1->env) &&
	  tp->p2->vtable->isValidCF(tp->p2->env));
}

Dan Hirsch's avatar
Dan Hirsch committed
static const HParserVtable xor_vt = {
  .parse = parse_xor,
  .isValidRegular = h_false,
  .isValidCF = xor_isValidCF,
const HParser* h_xor(const HParser* p1, const HParser* p2) {
  return h_xor__m(&system_allocator, p1, p2);
}
const HParser* h_xor__m(HAllocator* mm__, const HParser* p1, const HParser* p2) { 
  HTwoParsers *env = h_new(HTwoParsers, 1);
Dan Hirsch's avatar
Dan Hirsch committed
  env->p1 = p1; env->p2 = p2;
  HParser *ret = h_new(HParser, 1);
Dan Hirsch's avatar
Dan Hirsch committed
  ret->vtable = &xor_vt; ret->env = (void*)env;
  return ret;
}