-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatdj_names.ml
138 lines (126 loc) · 3.12 KB
/
atdj_names.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
(* Names *)
open Atdj_env
let to_camel_case s =
let res = String.copy s in
let offset = ref 0 in
let upper = ref true in
let f = function
| '_' ->
upper := true;
| ('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9') as x ->
upper := true;
res.[!offset] <- x;
incr offset
| _ as x ->
if !upper then (
res.[!offset] <- Char.uppercase x;
upper := false
) else
res.[!offset] <- x;
incr offset in
String.iter f s;
String.sub res 0 !offset
(* Translate type names into idiomatic Java class names. We special case
* `string', `int' and `bool' (see code). For the remainder, we remove
* underscores and capitalise any character that is immediately following
* an underscore or digit. We also capitalise the initial character
* e.g. "foo_bar42baz" becomes "FooBar42Baz". *)
let to_class_name str =
match str with
| "string" -> "String"
| "int" -> "Integer"
| "bool" -> "Boolean"
| "float" -> "Double"
| _ -> to_camel_case str
let java_keywords = [
"abstract";
"assert";
"boolean";
"break";
"byte";
"case";
"catch";
"char";
"class";
"const";
"continue";
"default";
"do";
"double";
"else";
"enum";
"extends";
"final";
"finally";
"float";
"for";
"goto";
"if";
"implements";
"import";
"instanceof";
"int";
"interface";
"long";
"native";
"new";
"package";
"private";
"protected";
"public";
"return";
"short";
"static";
"strictfp";
"super";
"switch";
"synchronized";
"this";
"throw";
"throws";
"transient";
"try";
"void";
"volatile";
"while";
]
let is_java_keyword =
let tbl = Hashtbl.create 200 in
List.iter (fun k -> Hashtbl.add tbl k ()) java_keywords;
fun k -> Hashtbl.mem tbl k
(*
Automatically append an underscore to a field name if it is a Java keyword.
Use the alternative provided as <java name ="..."> if available.
ATD field Java name
not_a_keyword not_a_keyword
class class_
class <java name="class_name"> class_name
not_a_keyword <java name="class"> class
*)
let get_java_field_name field_name annot =
let field_name =
if is_java_keyword field_name then
field_name ^ "_"
else
field_name
in
Atd_annot.get_field (fun s -> Some s) field_name ["java"] "name" annot
let get_java_variant_names field_name annot =
let lower_field_name = String.lowercase field_name in
let field_name =
if is_java_keyword lower_field_name then
field_name ^ "_"
else
field_name
in
let field_name =
Atd_annot.get_field (fun s -> Some s) field_name ["java"] "name" annot
in
let func_name = to_camel_case field_name in
let enum_name = String.uppercase field_name in
let private_field_name = String.lowercase field_name in
func_name, enum_name, private_field_name
let get_json_field_name field_name annot =
Atd_annot.get_field (fun s -> Some s) field_name ["json"] "name" annot
let get_json_variant_name field_name annot =
Atd_annot.get_field (fun s -> Some s) field_name ["json"] "name" annot