I am planning on supporting global state in my peg generator. The way Rats! 
does it with transactions looks pretty good to me but my question is how to 
actually implement the state operations in a manner that work with 
transactions. Specifically the state has to be pure in some sense so that it 
can be "undone" by an abort operation.

My first naive idea is to have some State class (maybe defined by the user) 
with a pointer to a child State class. The start() method of a transaction 
would create a new State object and set parent->next to it. Then the user can 
modify the new State object. commit() would be a no-op and abort() would reset 
the parent->next pointer to null.

In psuedo-code

start(){
  state = new State();
  current->next = state;
  return state;
}

abort(){
  current->next = null;
}

commit(){}

# blah = a b c
#            |  d e f
rule_blah(){
   # For each production, do a start/commit/abort sequence
   state = start();
   rule_a(); rule_b(); rule_c();
   commit();
   return
   fail: # if any of the rules failed somehow
   abort();

   state = start();
   rule_d(); rule_e(); rule_f();
   commit();
   return
   fail:
   abort();
}

_______________________________________________
PEG mailing list
[email protected]
https://lists.csail.mit.edu/mailman/listinfo/peg

Reply via email to