Skip to content
This repository
tree: cd0c3f8c85
Fetching contributors…

Cannot retrieve contributors at this time

file 73 lines (59 sloc) 2.804 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* FIXME: remove open *)
module B = Base
let (<|) f a = f a
let (|>) a f = f a
let ( @* ) g f x = g(f(x))
module O = Ocaml
module Cons = O.Cons
module G = Grammar
module T = Tools
module L = B.List

let get_define = function | G.Define (G.Constr (name, lst), pattern) -> name, lst | _ -> assert false
let get_raw = function | G.Raw (name, (pre1,opts), lst) -> name, lst | _ -> assert false

let compare_pattern_of_ getdr dr =
  let name, lst = getdr dr in
  let pt = if L.length lst > 0 then [O.PatAny] else [] in
  (O.PatTuple [O.PatConstructor ([Ident.source name],pt);
            O.PatConstructor ([Ident.source name],pt)],
   None,
   Cons.bool true)

let gen_get_compare_ name getdr lst =
  O.Let [O.Pat (O.PatVar (Ident.source ("compare_"^name))),
       O.Function ((L.map (compare_pattern_of_ getdr) lst)@[(O.PatAny,None,(Cons.bool false))])]

let gen_get_compare_msg = gen_get_compare_ "msg" get_define
let gen_get_compare_rawmsg = gen_get_compare_ "rawmsg" get_raw

let name_pattern_of_ getdr dr =
  let name, lst = getdr dr in
  let pt = if L.length lst > 0 then [O.PatAny] else [] in
  (O.PatConstructor ([Ident.source name],pt), None, Cons.string name)

let gen_get_name_ name getdr lst =
  O.Let [O.Pat (O.PatVar (Ident.source (Printf.sprintf "get_%s_name" name))), O.Function ((L.map (name_pattern_of_ getdr) lst))]

let gen_get_msg_name = gen_get_name_ "msg" get_define
let gen_get_rawmsg_name = gen_get_name_ "rawmsg" get_raw

let get_pattern_of_ getdr dr =
  let name, lst = getdr dr in
  (Printf.sprintf "\nlet get_%s rh =\n " name)^
    if L.length lst <> 0
    then
      let pat = Printf.sprintf "%s _v" name in
      (Printf.sprintf "match List.find_opt (function %s -> true | _ -> false) rh with\n | Some (%s) -> Some _v\n | _ -> None"
               pat pat)
    else
      (Printf.sprintf "match List.find_opt (function %s -> true | _ -> false) rh with\n | Some %s -> Some %s\n | _ -> None"
               name name name)

let gen_get_value_ getdr lst = O.Verbatim (String.concat "\n" (L.map (get_pattern_of_ getdr) lst))

let gen_get_msg_value = gen_get_value_ get_define
let gen_get_rawmsg_value = gen_get_value_ get_raw
Something went wrong with that request. Please try again.