Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (75 sloc) 2.583 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (* CF mli *)
19
20 module String = Base.String
21 let (@*) = InfixOperator.(@*)
22
23 type ext = string
24 type lang = ML | JS | C
25 type format = [ `compiled | `interpreted ]
26 type t = Language of lang * format
27
28 let mli = Language (ML, `interpreted)
29 let ml = Language (ML, `compiled)
30 let js = Language (JS, `compiled)
31 let c = Language (C, `compiled)
32
33 external fieldlang : t -> lang = "%field0"
34
35 let is_ml ml = (fieldlang ml) = ML
36 let is_js js = (fieldlang js) = JS
37 let is_c c = (fieldlang c) = C
38
39 let compare = Pervasives.compare
40
41 let of_string ?format s =
42 let dformat = Option.default `compiled format in
43 match String.lowercase s with
44 | "ml" | "ocaml" -> Some (Language (ML, dformat))
45 | "ml:top" when format <> Some `compiled -> Some (Language (ML, `interpreted))
46 | "js" | "javascript" -> Some (Language (JS, dformat))
47 | "c" -> Some (Language (C, dformat))
48 | _ -> None
49
50 let to_string (Language (l, c)) =
51 match l, c with
52 | ML, `compiled -> "ml"
53 | ML, `interpreted -> "ml:top"
54 | JS, _ -> "js"
55 | C, _ -> "c"
56
57 let pp fmt = Format.pp_print_string fmt @* to_string
58 (* one failure -> None *)
59 let of_list =
60 let rec stop_fold accu = function
61 | [] -> Some accu
62 | t::q ->
63 begin
64 match of_string t with
65 | None -> None
66 | Some lang ->
67 if List.exists (fun t -> t = lang) accu
68 then stop_fold accu q
69 else stop_fold (lang::accu) q
70 end
71 in stop_fold []
72
73 let parse s =
461365b [cleanup] Base.String: changed String.split to a much simpler String.…
Louis Gesbert authored
74 let li = String.slice_chars "/ ,{}" s in
fccc685 Initial open-source release
MLstate authored
75 of_list li
76
77 let gen_print f list = String.concat_map ~left:"{" ~right:"}" ", " f list
78
79 let print = gen_print to_string
80 let pp_list fmt t = Format.pp_print_string fmt (print t)
81
82 let formate f (Language (lg, _)) = Language (lg, f)
83
84 (* private *)
85 let lang (Language (lg, _)) = lg
86
87 let pp_meta fmt t =
88 let s =
89 match fieldlang t with
90 | ML -> "ml"
91 | JS -> "js"
92 | C -> "c"
93 in
94 Format.fprintf fmt "L.%s" s
Something went wrong with that request. Please try again.