1
- type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | DuplicateLiteral of string
1
+ type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | AtMostOneString | AtMostOneNumber | DuplicateLiteral of string
2
2
type error =
3
3
| InvalidVariantAsAnnotation
4
4
| Duplicated_bs_as
@@ -22,6 +22,8 @@ let report_error ppf =
22
22
| OnlyOneUnknown -> " An unknown case must be the only case with payloads."
23
23
| AtMostOneObject -> " At most one case can be an object type."
24
24
| AtMostOneArray -> " At most one case can be an array type."
25
+ | AtMostOneString -> " At most one case can be a string type."
26
+ | AtMostOneNumber -> " At most one case can be a number type (int or float)."
25
27
| DuplicateLiteral s -> " Duplicate literal " ^ s ^ " ."
26
28
)
27
29
@@ -146,9 +148,11 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
146
148
let module StringSet = Set. Make (String ) in
147
149
let string_literals = ref StringSet. empty in
148
150
let nonstring_literals = ref StringSet. empty in
149
- let arrays = ref 0 in
150
- let objects = ref 0 in
151
- let unknowns = ref 0 in
151
+ let arrayTypes = ref 0 in
152
+ let objectTypes = ref 0 in
153
+ let stringTypes = ref 0 in
154
+ let numberTypes = ref 0 in
155
+ let unknownTypes = ref 0 in
152
156
let addStringLiteral ~loc s =
153
157
if StringSet. mem s ! string_literals then
154
158
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
@@ -158,12 +162,16 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
158
162
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
159
163
nonstring_literals := StringSet. add s ! nonstring_literals in
160
164
let invariant loc =
161
- if ! unknowns <> 0 && (List. length blocks <> 1 )
165
+ if ! unknownTypes <> 0 && (List. length blocks <> 1 )
162
166
then raise (Error (loc, InvalidUntaggedVariantDefinition OnlyOneUnknown ));
163
- if ! objects > 1
167
+ if ! objectTypes > 1
164
168
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject ));
165
- if ! arrays > 1
169
+ if ! arrayTypes > 1
166
170
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneArray ));
171
+ if ! stringTypes > 1
172
+ then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString ));
173
+ if ! numberTypes > 1
174
+ then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber ));
167
175
() in
168
176
Ext_list. rev_iter consts (fun (loc , literal ) -> match literal.literal_type with
169
177
| Some (String s ) ->
@@ -185,15 +193,21 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
185
193
if isUntaggedDef then
186
194
Ext_list. rev_iter blocks (fun (loc , block ) -> match block.block_type with
187
195
| Some Unknown ->
188
- incr unknowns ;
196
+ incr unknownTypes ;
189
197
invariant loc
190
198
| Some Object ->
191
- incr objects ;
199
+ incr objectTypes ;
192
200
invariant loc
193
201
| Some Array ->
194
- incr arrays ;
202
+ incr arrayTypes ;
195
203
invariant loc
196
- | _ -> () )
204
+ | Some (IntType | FloatType ) ->
205
+ incr numberTypes;
206
+ invariant loc
207
+ | Some StringType ->
208
+ incr stringTypes;
209
+ invariant loc
210
+ | None -> () )
197
211
198
212
let names_from_type_variant ?(isUntaggedDef =false ) (cstrs : Types.constructor_declaration list ) =
199
213
let get_cstr_name (cstr : Types.constructor_declaration ) =
0 commit comments