Skip to content
This repository
tag: v1869
Fetching contributors…

Cannot retrieve contributors at this time

file 420 lines (359 sloc) 15.283 kb
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* @author Rudy Sicard *)
(*
Can be before renaming,

For internationalisation, when generating template
replace @i18n(@string(["computed_",@magic_to_string{"string"),"_yo"]) by
generated_fun(@magic_to_string{"string")) and collect generated_fun
or remove @i18n when no internationalisation package is known
replace @i18n(something) by something(@i18_lang)


Should be after renaming
After renaming
For computed String, when not generating template
replace @string(["computed_",@magic_to_string{"string"),"_yo"]) by sc_list(["computed_",@magic_to_string{"string"),"_yo"])

replace @i18n_lang by the relevant library call


For performance reason we group both @i18n and @string process, so everything is with uid

*)
let (|>) a f = f a

let i18n_pkg_name = "stdlib.core.i18n"
let default_pkg_ext = ".translation"
let linking_pkg_name = "linking"

let i18n_warn =
  WarningClass.create
    ~public:true
    ~name:"i18n"
    ~doc:"All i18n related warnings"
    ~err:false
    ~enable:true
    ()

let i18n_warn_existing =
  WarningClass.create
    ~parent:i18n_warn
    ~public:true
    ~name:"existing-package"
    ~doc:"Warn if the i18n-template is generated while an old version of the template is still there"
    ~err:false
    ~enable:true
    ()

let i18n_warn_missing =
  WarningClass.create
    ~parent:i18n_warn
    ~public:true
    ~name:"missing"
    ~doc:"Warn for missing translation"
    ~err:false
    ~enable:true
    ()

let i18n_warn_missing_template =
  WarningClass.create
    ~parent:i18n_warn_missing
    ~public:true
    ~name:"template"
    ~doc:"Warn if a i18n-template is missing"
    ~err:false
    ~enable:true
    ()

let i18n_warn_missing_translation_package =
  WarningClass.create
    ~parent:i18n_warn_missing
    ~public:true
    ~name:"package"
    ~doc:"Warn if a translation package is missing"
    ~err:true
    ~enable:true
    ()



let warning_set =
  WarningClass.Set.create_from_list [
    i18n_warn;
    i18n_warn_existing;
    i18n_warn_missing_template;
  ]

module SA = SurfaceAst
module SAC = SurfaceAstCons.ExprIdentCons

let copy_label v = {v with QmlLoc.notes = SurfaceAstCons.Fresh.id ()}

(* generate a call to directive i18n_lang (or equivalent form *)
(* when cleaning of js code must be done, shoud really use the directive to simplify code analysis *)
let i18n_lang () =
  let i18n_lang = SAC.E.ident (OpaMapToIdent.val_ Opacapi.I18n.lang) in
  SAC.E.applys i18n_lang []
  (* SAC.D.i18n_lang() *)
  (* SAC.E.string "WTF" *)


(* generate a call to String.flatten with the given expr list as parameter, need with_label *)
let flattened_string expr_list =
  match expr_list with
  | hd :: [] -> hd
  | hd :: _ ->
    SurfaceAstCons.with_same_pos hd (fun() ->
      let flatten = SAC.E.ident (OpaMapToIdent.val_ Opacapi.String.flatten) in
      let expr_list = SAC.E.list expr_list in
      let flattened = SAC.E.applys flatten [expr_list] in
      flattened
    )
  | _ -> failwith "Empty flattened_string list"

(* separate constant part and parameter part (inserts) of a string *)
let consts_and_expressions expr_list =
  List.partition (function
  |SA.Const SA.CString _,_ -> true
  |SA.Directive _,_ -> false
  |_ -> assert false
  ) expr_list

(* generate an ident for a computed string that depends on constant part of the string and on inserts positions *)
let translation_fun_ident consts =
  let digest = List.fold_left (fun acc e->
    match e with
    |SA.Const SA.CString s,_ -> Digest.string (acc ^ s)
    |_ -> assert false
  ) "" consts
  in Printf.sprintf "__i18n_%s" (Digest.to_hex digest)

(* adding a source ident here is ok when it is used to generate template file *)
let template_ident_cons = Ident.source

let translation_fun_body_default_case default_expr =
  let default_ident = template_ident_cons "_default" in
  let pat = SAC.P.var default_ident in
  (pat,default_expr)

(* substitute non const string expression by parameters *)
let subst_parameters expr_list parameters =
  let _, expr_list = Base.List.fold_left_map (fun acc e->
    match e with
    |SA.Const SA.CString _,_ -> acc,e
    | _ -> List.tl acc,(List.hd acc)
  ) parameters expr_list
  in expr_list

type ('a,'b) translation_fun = {
  id : string;
  parameters : string list; (* string or something else *)
  initial_expr : ('a,'b) SurfaceAst.expr;
  default_expr : ('a,'b) SurfaceAst.expr;
  body : ('a,'b) SurfaceAst.expr;
}

(* generate a translation body given the number of parameter and initial computed string, need with_label *)
let translation_fun_body initial_expr expr_list ()=
  let (consts,expressions) = consts_and_expressions expr_list in
  let nb_parameters = List.length expressions in
  let parameters = Base.List.init nb_parameters (fun i-> Printf.sprintf "p%d" (i+1)) in
  let args = List.map template_ident_cons parameters in
  let lambda_args = List.map SAC.P.var args in
  let string_inserts = List.map SAC.E.ident args in
  let default_expr = SAC.D.string (subst_parameters expr_list string_inserts) in
  let default_pattern_case = translation_fun_body_default_case default_expr in
  let match_ = SAC.E.match_ (i18n_lang()) [default_pattern_case] in
  let body = SAC.E.lambda lambda_args match_ in
  {
    id = translation_fun_ident consts;
    parameters;
    body ; default_expr ; initial_expr
  }

let collect_directives__i18n acc code =
  let fold (acc,v) f = OpaWalk.Code.fold f acc v in
  let process e expr_list = SurfaceAstCons.with_label (snd e)
    (translation_fun_body e expr_list)
  in
  let collect acc code =
    fold(acc,code)(fun acc (e,_) ->
      match e with
      (* @i18n("...") ==> add template translation package *)
      | SA.Directive (`i18n , [SA.Directive (`string, expr_list, _),_ as e], _) -> (process e expr_list)::acc
      | SA.Directive (`i18n , [SA.Const (SA.CString _) ,_ as e], _) -> (process e [e] )::acc
      | _ -> acc
    )
  in
  collect acc code

let warn_missing_template e i18n_dir i18n_pkg str_ident =
  QmlError.warning ~wclass:i18n_warn_missing_template (QmlError.Context.pos (QmlLoc.pos (snd e)))
    "A translation function is missing (%s),\nplease regenerate translation template packages:\n %a {%a}/*.opa"
    str_ident
    (Base.Format.pp_list "@," Base.Format.pp_print_string) i18n_pkg
    (Base.Format.pp_list "@," Base.Format.pp_print_string) i18n_dir

(* replace @i18n and @string directives by calls to the proper functions (String.flatten or translation) *)
let replace_directives__i18n__string ~i18n_dir ~i18n_pkg code =
  let enable_i18n = i18n_dir <> [] || i18n_pkg <> [] in
  let map v f = OpaWalk.Code.map_down f v in
  let translate expr_list = SurfaceAstCons.with_same_pos (List.hd expr_list) (fun()->
    let (consts,expressions) = consts_and_expressions expr_list in
    let str_ident = translation_fun_ident consts in
    try
      let ident = OpaMapToIdent.val_no_opacapi_check str_ident in
      let translation_fun_ident = SAC.E.ident ident in
      let translated = SAC.E.applys translation_fun_ident expressions in
      translated
    with Not_found -> (
      (* template not available, ignore @i18n mode *)
      warn_missing_template (List.hd expr_list) i18n_dir i18n_pkg str_ident;
      flattened_string expr_list
    )
  )
  in
  let process = if enable_i18n then translate else flattened_string in
  let rewrite code =
    map(code)(fun ((e,label) as v) ->
      match e with
      (* "..." ==> String_flatten(...) *)
      | SA.Directive (`string, [] , _) -> SAC.E.string ~label ""
      | SA.Directive (`string, expr_list, _) -> flattened_string expr_list

      (* @i18n("...") ==> String_flatten(...) OR translate_fun(parameters) *)
      | SA.Directive (`i18n , [SA.Directive (`string, expr_list, _),_], _) -> process expr_list
      | SA.Directive (`i18n , [SA.Const (SA.CString _),_ as string ], _) -> process [string]

      (* @i18n(expr) => expr(@i18n_lang) *)
      | SA.Directive (`i18n , [expr], _ ) -> SurfaceAstCons.with_label label (fun()->
        SAC.E.applys expr [i18n_lang ()]
      )

      | SA.Directive (`i18n , _ , _ ) -> assert false
      | _ -> v
    )
  in
  rewrite code

let pos_get_line pos = snd (FilePos.get_one_loc pos)

(* header of one translation entry *)
let generate_template_header f tf =
  let pos = QmlLoc.pos (snd tf.initial_expr) in
  Format.fprintf f "// Template for %s\n" (FilePos.get_file pos);
  Format.fprintf f "// %a\n" OpaPrint.readable_ident#expr tf.default_expr; (* printing e here can have some drawbacks, need to be solved => use string extracts ? *)
  Format.fprintf f "// string, %d\n" (pos_get_line pos)


let pp_parameter f s = Base.Format.fprintf f "%s:string" s

(* a translation function in opa, pure string *)
let generate_declaration_custom f tf =
  Format.fprintf f "%s(%a)= match I18n.lang()\n" tf.id (Base.Format.pp_list "," pp_parameter) tf.parameters;
  (*"{123} ->"*)
  Format.fprintf f " _ -> %a" OpaPrint.readable_ident#expr tf.default_expr

(* same from an AST *)
let generate_declaration f tf =
  let pat_id = SurfaceAstCons.with_same_pos tf.initial_expr (fun()-> SAC.P.var (template_ident_cons tf.id)) in
  Format.fprintf f "%a\n\n" OpaPrint.readable_ident#code_elt_node (SA.NewVal ([pat_id,tf.body],false))

(* generate the source of a translation package *)
let generate_opa_file name collection target =
  let f = Format.formatter_of_out_channel target in
  Format.fprintf f "package %s\n" name;
  Format.fprintf f "import %s\n" i18n_pkg_name;
  List.iter (fun tf ->
    (* here should check for an already present definition in old file *)
    Format.fprintf f "\n";
    generate_template_header f tf;
    generate_declaration_custom f tf;
    Format.fprintf f "\n\n";
  ) collection

(* TODO, if an Opa file already exists, then parse it and use it to extract code verbatim or make a merge thing *)
(* let _merge_opa_file name collection target old = assert false *)

let generate_po_file collection =
  let f = Format.formatter_of_out_channel stdout in
  List.iter (fun tf ->
    generate_template_header f tf;
    Format.printf "\nmsgid \"%a\"\n" OpaPrint.readable_ident#expr tf.default_expr;
    Format.printf "msgstr \"\"\n\n";
  ) collection

let current_package ?package () =
  let package = match package with
    | Some package -> package
    | None -> ObjectFiles.get_current_package_name()
  in if package="" then linking_pkg_name else package

let default_i18n_package ?package () = (current_package ?package ())^default_pkg_ext

module I18n = OpaEnv.I18n

let need_to_import_package ~options =
  let open I18n.Type in (* for I18n fields *)
  let opt = options.OpaEnv.i18n in
  (opt.pkg<> [] || opt.dir<>[]) && not(opt.template_opa || opt.template_po)

let warn_missing_package initial_package i18n_package =
  QmlError.warning ~wclass:i18n_warn_missing_translation_package
    (QmlError.Context.package initial_package)
    "Translation package %s is needed (by %s) but not provided"
    i18n_package
    initial_package

let import_package ?package ~exists ~options =
  let open I18n.Type in (* for I18n fields *)
  let should_import =
    match options.OpaEnv.i18n.pkg with
    | [] -> [default_i18n_package ?package ()]
    | v -> v
  in List.filter (fun i18n_package ->
    if exists i18n_package then true
    else (
      warn_missing_package (current_package ?package()) i18n_package;
      false
    )
  ) should_import

let may_import_package ?package ~exists ~options =
  if need_to_import_package ~options then (
    let ps = import_package ?package ~exists ~options in
    (*if ps <> [] then Base.Format.printf "I18n: importing %a\n" (Base.Format.pp_list "," Base.Format.pp_print_string) ps;*)
    ps
  ) else []

let backup_existing name =
  try
    let stat = Unix.stat name in
    let nname = Printf.sprintf "%s.%d" name (int_of_float stat.Unix.st_atime) in
    Unix.rename name nname;
    Some(nname)
  with Unix.Unix_error(Unix.ENOENT,_,_) -> None

let with_file conflict name f =
  let name = PathTransform.string_to_mysys name in
  let backup = backup_existing name in
  let c = open_out name in
  (try f c with _ -> ());
  close_out c;
  match backup with
  | None -> ()
  | Some b -> conflict name b


module SAP = SurfaceAstPassesTypes
let replace_directives__i18n__string ~i18n_dir ~i18n_pkg env =
  let lcodeNotUser = replace_directives__i18n__string ~i18n_dir ~i18n_pkg env.SAP.lcodeNotUser in
  let lcodeUser = replace_directives__i18n__string ~i18n_dir ~i18n_pkg env.SAP.lcodeUser in
  { env with SAP.
    lcodeNotUser = lcodeNotUser;
    lcodeUser = lcodeUser
  }

let collect_directives__i18n env =
  let acc = collect_directives__i18n [] env.SAP.lcodeNotUser in
  let acc = collect_directives__i18n acc env.SAP.lcodeUser in
  let cmp_id tf1 tf2 = Pervasives.compare tf1.id tf2.id in
  let cmp_pos =
    let pos tf = FilePos.get_one_loc (QmlLoc.pos (snd tf.initial_expr)) in
    (fun tf1 tf2 -> Pervasives.compare (pos tf1) (pos tf2))
  in
  acc
  |> List.stable_sort cmp_id (* need determist order (wrt initial relative order) for equal elements *)
  |> Base.List.uniq ~cmp:cmp_id (* TODO merge position *)
  |> List.sort cmp_pos

let process_directives__i18n__string ~options env =
  let open I18n.Type in (* for I18n fields *)
  let i18n = options.OpaEnv.i18n in
  if OpaEnv.i18n_template options then (
    begin match collect_directives__i18n env with
    | [] -> ()
    | tf :: _ as collection ->
      let target_dir = match i18n.dir with
        | dir :: _ -> dir
        | [] -> "."
      in
      let target_package = match i18n.pkg with
        | pkg :: _ -> pkg
        | [] -> default_i18n_package ()
      in
(* Printf.printf "I18n : Translation package name %s\n" target_package;*)
(* Printf.printf "I18n : Number of translation template %d\n" (List.length collection); flush stdout;*)
      let conflict target diff new_ old_ = (* TODO use merge function here *)
        QmlError.warning ~wclass:i18n_warn_existing (QmlError.Context.pos (QmlLoc.pos (snd tf.initial_expr)))
          "Old version of package %s was backup in %s\nYou should merge with the new version :\n%s %s %s %s | tee %s\n"
          target old_
          diff new_ new_ old_ new_
      in
      let dest_file_opa = Printf.sprintf "%s/%s" target_dir target_package in
      if i18n.template_opa then
        with_file (conflict target_package "meld") (dest_file_opa^".opa") (generate_opa_file target_package collection);
      if i18n.template_po then generate_po_file collection;
    end;
    exit 0
  ) else replace_directives__i18n__string ~i18n_dir:i18n.dir ~i18n_pkg:i18n.pkg env

let process_directives__i18n_lang ~options env =
  let _ = options in
  env
Something went wrong with that request. Please try again.