-
Notifications
You must be signed in to change notification settings - Fork 2
/
atdj_helper.ml
151 lines (141 loc) · 3.94 KB
/
atdj_helper.ml
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
(* Helper classes *)
open Printf
open Atdj_env
let output_atdj env =
let out = Atdj_trans.open_class env "Atdj" in
fprintf out "\
/**
* Common utility interface.
*/
public interface Atdj {
/**
* Get the JSON string representation, failing if some of the data
* was not initialized.
* @return The JSON string.
*/
String toJson() throws JSONException;
/**
* Write the JSON representation to a buffer, failing if some of the data
* was not initialized.
*/
void toJsonBuffer(StringBuilder out) throws JSONException;
}
";
close_out out
let output_util env =
let out = Atdj_trans.open_class env "Util" in
fprintf out "\
class Util {
// Extract the tag of sum-typed value
static String extractTag(Object value) throws JSONException {
if (value instanceof String)
return (String)value;
else if (value instanceof JSONArray)
return ((JSONArray)value).getString(0);
else throw new JSONException(\"Cannot extract type\");
}
// Is an option value a none?
static boolean isNone(Object value) throws JSONException {
return (value instanceof String) && (((String)value).equals(\"None\"));
}
// Is an option value a Some?
static boolean isSome(Object value) throws JSONException {
return (value instanceof JSONArray)
&& ((JSONArray)value).getString(0).equals(\"Some\");
}
/*
Encode a JSON string into a buffer
*/
static void writeJsonString(StringBuilder out, String s) {
out.append(\"\\\"\");
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
switch (c) {
case '\\b':
out.append(\"\\\\b\");
break;
case '\\f':
out.append(\"\\\\f\");
break;
case '\\n':
out.append(\"\\\\n\");
break;
case '\\r':
out.append(\"\\\\r\");
break;
case '\\t':
out.append(\"\\\\t\");
break;
case '\\\\':
out.append(\"\\\\\\\\\");
break;
case '\"':
out.append(\"\\\\\\\"\");
break;
default:
if (c < 32 || c == 127)
out.append(String.format(\"\\\\u%%04x\", (int) c));
else
out.append(c);
}
}
out.append(\"\\\"\");
}
static String jsonStringOfString(String s) {
StringBuilder out = new StringBuilder();
writeJsonString(out, s);
return out.toString();
}
// Unescape escaped backslashes and double quotations.
// All other escape sequences are considered invalid
// (this is probably too strict).
static String unescapeString(String str) throws JSONException {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == '\\\\') {
if (i == str.length() - 1 ||
(str.charAt(i + 1) != '\\\\' && str.charAt(i + 1) != '\"'))
throw new JSONException(\"Invalid escape\");
else {
buf.append(str.charAt(i + 1));
++i;
}
} else {
buf.append(str.charAt(i));
}
}
return buf.toString();
}
}
";
close_out out
let output_package_javadoc env (loc, annots) =
let out = open_out (env.package_dir ^ "/" ^ "package.html") in
output_string out "<body>\n";
let from_doc_para acc para =
List.fold_left
(fun acc -> function
| `Text text -> text :: acc
| `Code _ -> failwith "Not yet implemented: code in javadoc comments"
)
acc
para in
let from_doc = function
| `Text blocks ->
List.fold_left
(fun acc -> function
| `Paragraph para -> from_doc_para acc para
| `Pre _ ->
failwith "Not yet implemented: \
preformatted text in javadoc comments"
)
[]
blocks in
(match Ag_doc.get_doc loc annots with
| Some doc ->
let str = String.concat "\n<p>\n" (List.rev (from_doc doc)) in
output_string out str
| _ -> ()
);
output_string out "\n</body>";
close_out out