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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
(***********************************************************************)
(* *)
(* MLTk, Tcl/Tk interface of OCaml *)
(* *)
(* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis *)
(* projet Cristal, INRIA Rocquencourt *)
(* Jacques Garrigue, Kyoto University RIMS *)
(* *)
(* Copyright 2002 Institut National de Recherche en Informatique et *)
(* en Automatique and Kyoto University. All rights reserved. *)
(* This file is distributed under the terms of the GNU Library *)
(* General Public License, with the special exception on linking *)
(* described in file LICENSE found in the OCaml source tree. *)
(* *)
(***********************************************************************)
open Tables;;
open Format;;
let escape_string s =
let more = ref 0 in
for i = 0 to String.length s - 1 do
match s.[i] with
| '\\' | '"' -> incr more
| _ -> ()
done;
if !more = 0 then s else
let res = String.create (String.length s + !more) in
let j = ref 0 in
for i = 0 to String.length s - 1 do
let c = s.[i] in
match c with
| '\\' | '"' -> res.[!j] <- '\\'; incr j; res.[!j] <- c; incr j
| _ -> res.[!j] <- c; incr j
done;
res;;
let escape_char c = if c = '\'' then "\\'" else String.make 1 c;;
let print_quoted_string s = printf "\"%s\"" (escape_string s);;
let print_quoted_char c = printf "'%s'" (escape_char c);;
let print_quoted_int i =
if i < 0 then printf "(%d)" i else printf "%d" i;;
let print_quoted_float f =
if f <= 0.0 then printf "(%f)" f else printf "%f" f;;
(* Iterators *)
let print_list f l =
printf "@[<1>[";
let rec pl = function
| [] -> printf "@;<0 -1>]@]"
| [x] -> f x; pl []
| x :: xs -> f x; printf ";@ "; pl xs in
pl l;;
let print_array f v =
printf "@[<2>[|";
let l = Array.length v in
if l >= 1 then f v.(0);
if l >= 2 then
for i = 1 to l - 1 do
printf ";@ "; f v.(i)
done;
printf "@;<0 -1>|]@]";;
let print_option f = function
| None -> print_string "None"
| Some x -> printf "@[<1>Some@ "; f x; printf "@]";;
let print_bool = function
| true -> print_string "true" | _ -> print_string "false";;
let print_poly x = print_string "<poly>";;
(* Types of the description language *)
let rec print_mltype = function
| Unit -> printf "Unit" | Int -> printf "Int" | Float -> printf "Float"
| Bool -> printf "Bool" | Char -> printf "Char" | String -> printf "String"
| List m -> printf "@[<1>(%s@ " "List"; print_mltype m; printf ")@]"
| Product l_m ->
printf "@[<1>(%s@ " "Product"; print_list print_mltype l_m; printf ")@]"
| Record l_t_s_m ->
printf "@[<1>(%s@ " "Record";
print_list
(function (s, m) ->
printf "@[<1>("; print_quoted_string s; printf ",@ "; print_mltype m;
printf ")@]")
l_t_s_m;
printf ")@]"
| UserDefined s ->
printf "@[<1>(%s@ " "UserDefined"; print_quoted_string s; printf ")@]"
| Subtype (s, s0) ->
printf "@[<1>(%s@ " "Subtype"; printf "@[<1>("; print_quoted_string s;
printf ",@ "; print_quoted_string s0; printf ")@]"; printf ")@]"
| Function m ->
printf "@[<1>(%s@ " "Function"; print_mltype m; printf ")@]"
| As (m, s) ->
printf "@[<1>(%s@ " "As"; printf "@[<1>("; print_mltype m; printf ",@ ";
print_quoted_string s; printf ")@]"; printf ")@]";;
let rec print_template = function
| StringArg s ->
printf "@[<1>(%s@ " "StringArg"; print_quoted_string s; printf ")@]"
| TypeArg (s, m) ->
printf "@[<1>(%s@ " "TypeArg"; printf "@[<1>("; print_quoted_string s;
printf ",@ "; print_mltype m; printf ")@]"; printf ")@]"
| ListArg l_t ->
printf "@[<1>(%s@ " "ListArg"; print_list print_template l_t;
printf ")@]"
| OptionalArgs (s, l_t, l_t0) ->
printf "@[<1>(%s@ " "OptionalArgs"; printf "@[<1>(";
print_quoted_string s; printf ",@ "; print_list print_template l_t;
printf ",@ "; print_list print_template l_t0; printf ")@]"; printf ")@]";;
(* Sorts of components *)
let rec print_component_type = function
| Constructor -> printf "Constructor" | Command -> printf "Command"
| External -> printf "External";;
(* Full definition of a component *)
let rec print_fullcomponent = function
{component = c; ml_name = s; var_name = s0; template = t; result = m;
safe = b;
} ->
printf "@[<1>{"; printf "@[<1>component =@ "; print_component_type c;
printf ";@]@ "; printf "@[<1>ml_name =@ "; print_quoted_string s;
printf ";@]@ "; printf "@[<1>var_name =@ "; print_quoted_string s0;
printf ";@]@ "; printf "@[<1>template =@ "; print_template t;
printf ";@]@ "; printf "@[<1>result =@ "; print_mltype m; printf ";@]@ ";
printf "@[<1>safe =@ "; print_bool b; printf ";@]@ "; printf "@,}@]";;
(* components are given either in full or abbreviated *)
let rec print_component = function
| Full f -> printf "@[<1>(%s@ " "Full"; print_fullcomponent f; printf ")@]"
| Abbrev s ->
printf "@[<1>(%s@ " "Abbrev"; print_quoted_string s; printf ")@]";;
(* A type definition *)
(*
requires_widget_context: the converter of the type MUST be passed
an additional argument of type Widget.
*)
let rec print_parser_arity = function
| OneToken -> printf "OneToken" | MultipleToken -> printf "MultipleToken";;
let rec print_type_def = function
{parser_arity = p; constructors = l_f; subtypes = l_t_s_l_f;
requires_widget_context = b; variant = b0;
} ->
printf "@[<1>{"; printf "@[<1>parser_arity =@ "; print_parser_arity p;
printf ";@]@ "; printf "@[<1>constructors =@ ";
print_list print_fullcomponent l_f; printf ";@]@ ";
printf "@[<1>subtypes =@ ";
print_list
(function (s, l_f0) ->
printf "@[<1>("; print_quoted_string s; printf ",@ ";
print_list print_fullcomponent l_f0; printf ")@]")
l_t_s_l_f;
printf ";@]@ "; printf "@[<1>requires_widget_context =@ "; print_bool b;
printf ";@]@ "; printf "@[<1>variant =@ "; print_bool b0; printf ";@]@ ";
printf "@,}@]";;
let rec print_module_type = function
| Widget -> printf "Widget" | Family -> printf "Family";;
let rec print_module_def = function
{module_type = m; commands = l_f; externals = l_f0; } ->
printf "@[<1>{"; printf "@[<1>module_type =@ "; print_module_type m;
printf ";@]@ "; printf "@[<1>commands =@ ";
print_list print_fullcomponent l_f; printf ";@]@ ";
printf "@[<1>externals =@ "; print_list print_fullcomponent l_f0;
printf ";@]@ "; printf "@,}@]";;
|