     1 id set-global 0
   ] if* ;
 
-: init-parser ( parser -- parser )
-  #! Set the delegate for the parser. Equivalent parsers
-  #! get a delegate with the same id.
-  dup clone delegates [
-    drop next-id f <parser> 
-  ] cache over set-delegate ;
+: wrap-peg ( peg -- parser )
+  #! Wrap a parser tuple around the peg object.
+  #! Look for an existing parser tuple for that
+  #! peg object.
+  peg-cache [
+    f next-id parser boa 
+  ] cache ;
 
 TUPLE: token-parser symbol ;
 


TUPLE: sp-parser p1 ;

M: sp-parser (compile) ( parser -- quot )
  p1>> compiled-parser 1quotation '[ 
    input-slice left-trim-slice input-from pos set @ 
  ] ;

TUPLE: delay-parser quot ;

M: delay-parser (compile) ( parser -- quot )
  #! For efficiency we memoize the quotation.
  #! This way it is run only once and the 
  #! parser constructed once at run time.

: check-ensure ( old-input result -- result )
  [ ignore <parse-result> ] [ drop f ] if ;

M: ensure-parser (compile) ( peg -- quot )
  p1>> compiled-parser 1quotation '[ input-slice @ check-ensure ] ;

TUPLE: ensure-not-parser p1 ;
