parameters.ml 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
(* TEMPORARY clean up and write an .mli file *)

open Syntax
open Positions

let app p ps =
  match ps with
  | [] ->
      ParameterVar p
  | _ ->
      ParameterApp (p, ps)

let unapp = function
  | ParameterVar x ->
      (x, [])
  | ParameterApp (p, ps) ->
      (p, ps)
31 32 33
  | ParameterAnonymous _ ->
      (* Anonymous rules are eliminated early on. *)
      assert false
34 35 36 37 38 39

let rec map f = function
  | ParameterVar x ->
      ParameterVar (f x)
  | ParameterApp (p, ps) ->
      ParameterApp (f p, List.map (map f) ps)
40 41 42
  | ParameterAnonymous _ ->
      (* Anonymous rules are eliminated early on. *)
      assert false
43 44 45 46 47 48

let rec fold f init = function
  | ParameterVar x ->
      f init x
  | ParameterApp (p, ps) ->
      f (List.fold_left (fold f) init ps) p
49 50 51
  | ParameterAnonymous _ ->
      (* Anonymous rules are eliminated early on. *)
      assert false
52 53

let identifiers m p =
54
  fold (fun accu x -> StringMap.add x.value x.position accu) m p
55 56 57

type t = parameter

58
let rec equal x y =
59
  match x, y with
60 61
    | ParameterVar x, ParameterVar y ->
        x.value = y.value
62
    | ParameterApp (p1, p2), ParameterApp (p1', p2') ->
63
        p1.value = p1'.value && List.for_all2 equal p2 p2'
64 65 66
    | _ ->
        (* Anonymous rules are eliminated early on. *)
        false
67 68 69 70 71

let hash = function
  | ParameterVar x
  | ParameterApp (x, _) ->
      Hashtbl.hash (Positions.value x)
72 73 74
  | ParameterAnonymous _ ->
      (* Anonymous rules are eliminated early on. *)
      assert false
75 76

let position = function
77
  | ParameterVar x
78 79
  | ParameterApp (x, _) ->
      Positions.position x
80 81
  | ParameterAnonymous bs ->
      Positions.position bs
82 83 84

let with_pos p =
  Positions.with_pos (position p) p
85 86 87 88 89 90 91 92

let rec print = function
  | ParameterVar x ->
      x.value
  | ParameterApp (x, ps) ->
      x.value ^ "(" ^ Misc.separated_list_to_string print ", " ps ^ ")"
  | ParameterAnonymous _ ->
      assert false