2022-08-03 18:02:13 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley <emile.rolley@tuta.io>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 13:17:42 +03:00
|
|
|
include Stdlib.String
|
|
|
|
|
2022-08-03 18:02:13 +03:00
|
|
|
let to_ascii : string -> string = Ubase.from_utf8
|
2022-11-24 20:00:45 +03:00
|
|
|
let is_uppercase_ascii = function 'A' .. 'Z' -> true | _ -> false
|
2022-08-03 18:02:13 +03:00
|
|
|
|
|
|
|
let begins_with_uppercase (s : string) : bool =
|
2022-11-21 13:17:42 +03:00
|
|
|
"" <> s && is_uppercase_ascii (get (to_ascii s) 0)
|
2022-08-03 18:02:13 +03:00
|
|
|
|
|
|
|
let to_snake_case (s : string) : string =
|
2024-03-05 19:59:38 +03:00
|
|
|
let out = Buffer.create (2 * length s) in
|
|
|
|
s
|
|
|
|
|> to_ascii
|
2022-11-21 13:17:42 +03:00
|
|
|
|> iteri (fun i c ->
|
2024-03-05 19:59:38 +03:00
|
|
|
if is_uppercase_ascii c && 0 <> i then Buffer.add_char out '_';
|
|
|
|
Buffer.add_char out (Char.lowercase_ascii c));
|
|
|
|
Buffer.contents out
|
2022-08-03 18:02:13 +03:00
|
|
|
|
|
|
|
let to_camel_case (s : string) : string =
|
2024-03-05 19:59:38 +03:00
|
|
|
let last_was_underscore = ref true in
|
|
|
|
let out = Buffer.create (length s) in
|
|
|
|
s
|
|
|
|
|> to_ascii
|
|
|
|
|> iter (function
|
|
|
|
| '_' -> last_was_underscore := true
|
|
|
|
| c ->
|
|
|
|
Buffer.add_char out
|
|
|
|
(if !last_was_underscore then Char.uppercase_ascii c else c);
|
|
|
|
last_was_underscore := false);
|
|
|
|
Buffer.contents out
|
2022-11-22 22:57:59 +03:00
|
|
|
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
let remove_prefix ~prefix s =
|
|
|
|
if starts_with ~prefix s then
|
|
|
|
let plen = length prefix in
|
|
|
|
sub s plen (length s - plen)
|
|
|
|
else s
|
|
|
|
|
2023-07-07 15:48:53 +03:00
|
|
|
(* Note: this should do, but remains incorrect for combined unicode characters
|
|
|
|
that display as one (e.g. `e` + postfix `'`). We should switch to Uuseg at
|
|
|
|
some poing *)
|
|
|
|
let width s =
|
|
|
|
let len = length s in
|
|
|
|
let rec aux ncols i =
|
|
|
|
if i >= len then ncols
|
|
|
|
else if get s i = '\t' then aux (ncols + 8) (i + 1)
|
|
|
|
else aux (ncols + 1) (i + Uchar.utf_decode_length (get_utf_8_uchar s i))
|
|
|
|
in
|
|
|
|
aux 0 0
|
|
|
|
|
2023-07-12 12:48:46 +03:00
|
|
|
let format ppf s = Format.pp_print_as ppf (width s) s
|
2023-07-11 18:10:00 +03:00
|
|
|
|
2023-07-12 12:48:46 +03:00
|
|
|
module Arg = struct
|
|
|
|
include Stdlib.String
|
|
|
|
|
|
|
|
let format = format
|
|
|
|
end
|
|
|
|
|
|
|
|
module Set = Set.Make (Arg)
|
|
|
|
module Map = Map.Make (Arg)
|