Source

oni / cf / cf_xdfa.ml

(*---------------------------------------------------------------------------*
  $Change$
  Copyright (C) 2011, james woodyatt
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
    Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    
    Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  OF THE POSSIBILITY OF SUCH DAMAGE. 
 *---------------------------------------------------------------------------*)

module N_set = Cf_rbtree.Set(Cf_ordered.Int_order)
module N_map = Cf_rbtree.Map(Cf_ordered.Int_order)

let identity x = x

module type Symbol = sig
    type t and 'a map
    val map: (t -> 'a) -> 'a map
    val get: 'a map -> t -> 'a
end

module type T = sig
    module S: Symbol
    
    type 'a x
    
    val nil: 'a x
    
    type 'a r
end

module Create(S: Symbol) : (T with module S = S) = struct
    module S = S
    
    class virtual ['a] satisfier state =
        object(_:'self)
            constraint 'f = int -> (S.t, 'a) Cf_llscan.t
            
            val state_ = state
            
            method state = state_
            method follow u = {< state_ = N_set.union state_ u >}
            method virtual edge: S.t -> N_set.t -> N_set.t
            method accept = (None : 'f option)
        end
    
    let literal c =
        object
            inherit ['r] satisfier N_set.nil
            method edge n u = if n = c then N_set.union state_ u else u
        end
        
    let mapped f =
        object
            inherit ['r] satisfier N_set.nil
            method edge n u = if f n then N_set.union state_ u else u
        end
    
    type 's y = {
        y_counter: int;
        y_first: N_set.t;
        y_last: N_set.t;
        y_follow: 's N_map.t -> 's N_map.t;
    } constraint 's = 'a #satisfier
    
    type 's w = {
        w_null: bool;
        w_cons: int -> 's y;
    }
    
    type 'a x = 'a satisfier w
    
    let nil = {
        w_null = true;
        w_cons = fun i -> {
            y_counter = i;
            y_first = N_set.nil;
            y_last = N_set.nil;
            y_follow = identity;
        }
    }
    
    let expr n = {
        w_null = false;
        w_cons = fun i ->
            let s = N_set.singleton i in {
                y_counter = succ i;
                y_first = s;
                y_last = s;
                y_follow = fun m -> N_map.replace (i, n) m;
            }
    }
    
    type 'a r = 'a satisfier w
end

(*--- $File$ ---*)