Skip to content
This repository
tree: 11f17b2726
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1435 lines (1243 sloc) 50.967 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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
(*
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/>.
*)

(* depends *)
module String = BaseString
module List = BaseList
module Char = BaseChar

(* TODO remove *)
open Printf
open SurfaceAst
module Cons = SurfaceAstCons.StringCons

module Q = QmlAst

let (|>) = InfixOperator.(|>)

(* cf mli *)

(*
* Summary:
* - not ast related functions
* - errors
* - hints
* - fresh
* - annotations
* - tuples, records
* - identifiers
* - constants
* - functions
* - operators
* - datatype
* - constructs
* - functions expected from the stdlib
* - pattern
* - type const
* - type vars
* - types
* - directives
* - toplevel
* - others
* - actions action.trx
* - css css.trx
* - inlined css parser_xml.trx
* - trx trx.trx
*)

(* variant types are a nightmare *)
type ('a,'b) coerced_expr = ('a, [> `coerce ] as 'b) expr
type ('a,'b) coerced_expr_node = ('a, [> `coerce ] as 'b) expr_node

(* Variant types are a nightmare without smart alias ;) *)

(** An expression resulting of parsing *)
type parsing_expr = (nonuid, parsing_directive) expr

(** An node resulting of parsing *)
type parsing_node = (nonuid, parsing_directive) expr_node

(*
* not specific
*)
let cur2 f x y = f(x,y)
let unc2 f (x,y) = f x y
let map_tuple4 f (a,b,c,d) =
  let a = f a
  and b = f b
  and c = f c
  and d = f d in
    (a,b,c,d)

(*
* Errors
*)

let filename = ref "dummy_filename"
let offset2linecol offset = FilePos.get_pos !filename offset
let offset2string offset = FilePos.to_string (FilePos.make_pos !filename offset offset)
let annot2loc annot =
  let start = FilePos.get_first_char annot.QmlLoc.pos in
  offset2linecol start

exception Specific_parse_error of (FilePos.pos * string)

(* FIXME: see ErrorUtils later *)
let const_ty_to_string = function
      | TypeConst ty ->
          begin
            match ty with
              | TyInt -> Opacapi.Types.int
              | TyFloat -> Opacapi.Types.float
              | TyString -> Opacapi.Types.string
          end
      | _ -> assert false
let isolate s = sprintf "`%s'" s

let warning1 s annot =
  OManager.printf "@{<yellow>Warning@}:\n%s, %s@." (FilePos.to_string annot.QmlLoc.pos) s

let warning ~level:_ s =
  OManager.printf "@{<yellow>Warning@}: %s@." s

let error1 s annot =
  let str = sprintf "%s, %s" (FilePos.to_string annot.QmlLoc.pos) s in
  raise (Specific_parse_error (annot.QmlLoc.pos,str))

let error_comment = error1 "you start an unterminated comment (the `/*' is not matched by a `*/')."
let error_string = error1 "you start an unterminated string (the `\"' is not matched by a closing `\"')."
let error_char_escape c = error1 (sprintf "the escape \\%s is illegal. The legal escapes are: \\{, \\}, \\n, \\t, \\r, \\', \\\", \\\\, \\<integer>, \\uXXXX, \\UXXXXXXXX." (Char.escaped c))
let error_fun_space = error1 (sprintf "you must not put a space between a function and its parameters.")
let error_directive_not_good_arguments (s,pos) = error1 (sprintf "`@%s' expects arguments." s) pos
let error_directive mess (s,pos) = error1 (sprintf "Directive `@%s' expects %s." s mess) pos
let error_static_record = error_directive "a static record (ie. an expression surrounded by curly braces)"
let error_static_string = error_directive "a static string"
let error_not_a_directive (s,pos) = error1 (sprintf "`@%s' is not a valid directive." s) pos
let error_directive_wrong_arguments_type (s,pos) = error1 (sprintf "`@%s' wasn't given the right arguments." s) pos
let error_directive_number_argument min ?(max=min) got (s,pos) =
  if max = min then error1 (sprintf "`@%s' expected %d argument%s, but received %d." s min (if min < 2 then "" else "s") got) pos
  else error1 (sprintf "`@%s' expected between %d and %d arguments, but received %d." s min max got) pos

let error_char_overflow = error1 "you are giving a too large value: only character codes between 0 and 255 are accepted."
let error_int_overflow = error1 (Printf.sprintf "you are giving a too large integer literal: only values between %d and %d are accepted." min_int max_int)
let error_neither_ident_nor_call = error1 "the term is not a function call (the parenthesized expression is malformed). If it wasn't meant to be a function call, you need to separate the two expressions with spaces."
let error_redefinition_basic_type (ty,label) =
  let s = const_ty_to_string ty in
  error1 (sprintf "you are trying to redefine the basic type %s. If you really want that, use `%s` instead." s s) label
let error_bad_compare = error1 (sprintf "found a binding as condition. Did you mean `==' rather than `=' ?")
let error_static_xml pos =
  error1 "Directive `@xml' expects some static xml." pos
let error_consecutive_arrow pos =
  error1 "You have two consecutive arrows, this is a too ambiguous construct. If this is what you meant, add parentheses." pos
let error_sliced_expr =
  error1 "The directive @sliced_expr expects a static record with exactly the field server and the field client."
let error_db_file_without_slash (s, pos) = error1 (sprintf "the path to database files `%s' should contain a slash, e.g., './%s'" s s) pos
let error_conflicting_type_def_visibility (vis1, vis2, pos) =
   let string_of_visibility = function
     | SurfaceAst.TDV_abstract -> "@abstract"
     | SurfaceAst.TDV_private -> "@private"
     | SurfaceAst.TDV_public ->
         (* Since visibility "public" is the default and neutral element, it
can't cause a conflict, hence an error. So it should never appear
in such an error message. *)
         assert false in
   error1
     (sprintf
       "Type definition can't be specified %s and %s at the same time."
       (string_of_visibility vis1) (string_of_visibility vis2))
     pos

(*
* Hints
*)

let file_content_of_annot annot =
  let filename = FilePos.get_file annot.QmlLoc.pos in
  let start = FilePos.get_first_char annot.QmlLoc.pos in
  start, filename
type hint = [ `function_call of QmlLoc.annot | `declaration of QmlLoc.annot | `same_indent of (QmlLoc.annot * QmlLoc.annot) | `same_indents of QmlLoc.annot list ]
let hints = ref ([] : hint list)
let hints2 = ref ([] : hint list)
let push_hint v = hints := v :: !hints
let clear_hints () = hints2 := !hints; hints := []

let print_hints () =
  List.iter_right
    (function
       | `function_call pos -> warning1 (sprintf "there is only one space between the callable expression and the left parenthesis. It you wanted to call the expression, remove the space.") pos
       | `declaration annot ->
           let offset, _filename = file_content_of_annot annot in
           let _, column = offset2linecol offset in
           if column <> 0 then
             warning ~level:0 (sprintf "the declaration that begins at %s is indented. Did you forget a %s?\n" (offset2string offset) (isolate "do"))
       | `same_indent (annot1,annot2) ->
           (* FIXME: factorize with the following one *)
           let off1,file1 = file_content_of_annot annot1 in
           let off2,file2 = file_content_of_annot annot2 in
           assert (file1 = file2);
           let line1, col1 = offset2linecol off1 in
           let line2, col2 = offset2linecol off2 in
           if line1 <> line2 && col1 <> col2 then
             warning ~level:0 (sprintf "this local do/binding/open is not correctly indented:\nThe first part is at %s, the second at %s.\n" (offset2string off1) (offset2string off2))
       | `same_indents annots ->
           let offs,files = List.split (List.map file_content_of_annot annots) in
           match files with
             | [] -> ()
             | file :: t ->
                 assert (List.for_all ((=) file) t);
                 let rec aux ~resynchronize = function
                   | [] | [_] -> ()
                   | off1 :: off2 :: t ->
                       let line1, col1 = offset2linecol off1 in
                       let line2, col2 = offset2linecol off2 in
                       if line1 <> line2 then (
                         if col1 <> col2 then (
                           if not resynchronize then
                             warning ~level:0 (sprintf "two elements of a list of bindings are not correctly indented wrt each other:\nthe first part is at %s, the second at %s.\n%!" (offset2string off1) (offset2string off2));
                           aux ~resynchronize:true (off1 :: t)
                         ) else
                           aux ~resynchronize:false (off2 :: t)
                       ) else
                         aux ~resynchronize:false (off1 :: t)
                       in
                 aux ~resynchronize:false offs
    ) (!hints @ !hints2)

(*
* Generation of fresh stuff
*)

let fresh_id = SurfaceAstCons.Fresh.id



(* ************************************************************************** *)
(** {b Descr}: Generator for fresh *type* variables to be inserted by the
parsing.
ATTENTION, these type variables are labelled differently from those
generated after parsign passe (i.e. during parsers transformation and
typechecking) to avoid collision between them. In effect, if a source file
didn't change, its parse is cached, hence the parser is not ran on it,
hence the variable counter is not started and during parsers transformation
and typechecking passes, type variables will get generated startign from 0.
If unfortunatly such a variable gets created in the same scope than a
previous one inserted during the parse, they will conflict. This is revealed
by raising of type error since the 2 variable that should not have been
related are related, hence unification may encounter incompatible
instantiations for these 2 variables ... being in fact the same. *)
(* ************************************************************************** *)
let fresh_variable = Fresh.fresh_factory (sprintf "opaparse_v_%d")



let fresh_name = SurfaceAstCons.Fresh.old_name


(*
* Dealing with annotations
*)

let annot pos = {
  QmlLoc.pos = pos ;
  QmlLoc.notes = fresh_id () ;
}

let builtin () = annot (FilePos.nopos "Parser_utils.builtin")
let pos_only filename start stop =
  let pos = FilePos.make_pos filename start stop in
  annot pos
let decorate filename result start stop =
  (result, pos_only filename start stop)

(**Remove annotation from a node.

This is sometimes useful when a production needs to be split in several
rules due to precedence issues, as some nodes would need end up annotated
several times.*)
let undecorate (node, _label) = node
let label (_, label) : QmlLoc.annot = label
let copy_label label =
  {label with QmlLoc.notes = fresh_id ()}
let nlabel e = copy_label (label e)

let union_annot annot1 annot2 =
  let pos = FilePos.merge_pos_for_parser annot1.QmlLoc.pos annot2.QmlLoc.pos in
  {QmlLoc.pos = pos; QmlLoc.notes = fresh_id ()}
let union_annot2 t1 t2 = union_annot (label t1) (label t2)
let union_annot_list args =
  match args with
    | [] -> assert false
    | h :: _ -> union_annot2 h (List.last args)
let union_annot_list_snd args =
  match args with
    | [] -> assert false
    | (_,h) :: _ -> union_annot2 h (snd (List.last args))

let wrap f label = (f label, copy_label label)
let wrap_e f e = (f e, nlabel e)
let map_annot f (a,label) = (f a, label)


(*
* Dealing with tuples and records, and what is encoded the same way in the AST
* (function args, for instance)
*)

(**
Encode a tuple as a record.

A tuple is represented as a list of [(name, value)], with type [string option * 'a].
If [name] is [None], the user hasn't manually given a name to the field. We name the
first such field [f1], the second one [f2], etc. If [name] is [Some x], we keep name
[x]. At this stage, we do not check whether a name appears twice.
*)
let encode_tuple_as_record l =
  let rec aux i acc = function
    | [] -> List.rev acc
    | (None, typ)::t -> aux (i + 1) ((sprintf "f%d" i, typ)::acc) t
    | (Some n, typ)::t -> aux i ((n, typ)::acc) t
  in aux 1 [] l

let encode_tuple l : (string * _) list =
  let rec aux i acc = function
    | [] -> List.rev acc
    | typ::t -> aux (i + 1) ((sprintf "f%d" i, typ)::acc) t
  in aux 1 [] l
let encode_record l : (string * _) list = l

let encode_tuple_pos args =
  assert (args <> []);
  (encode_tuple args, union_annot_list args)
let encode_record_pos args : (string * _) list QmlLoc.label =
  assert (args <> []);
  (encode_record args, union_annot_list_snd args)

(* transform a list of arguments/parameters into its ast representation *)
let encode_args_as_record = encode_tuple
let encode_args_as_record_pos = encode_tuple_pos




(*
* Utils on identifiers
*)

let var_to_patvar (ident, label) =
  (PatVar {ident=ident;directives=[]}, label)
let var_to_exprvar (ident, label) =
  (Ident ident, label)
let patident s label = (PatVar {ident=s;directives=[]}, label)
let patvar = patident
let ident s label = (Ident s, label)
let fresh_ident_pat label =
  let n = fresh_name () in
    (var_to_exprvar (n, copy_label label), var_to_patvar (n, copy_label label))


(*
* Utils on types
*)
let row_t_tuple ?rowvar tyl = let (l2,label) = encode_tuple_pos tyl in (TyRow (l2,rowvar), copy_label label)
let row_t_record ?rowvar styl = let (l2,label) = encode_record_pos styl in (TyRow (l2,rowvar), copy_label label)
let row_t tyl label = (TyRow (encode_tuple tyl,None), copy_label label)
let arrow_t_node tyl ty label = (row_t tyl label, ty)
let arrow_t tyl ty label = (arrow_t_node tyl ty label, copy_label label)
let arrow tyl ty label = TypeArrow (arrow_t_node tyl ty label)
let arrow2 ty1 ty2 label = arrow [ty1] ty2 label
let typenamed name args label =
  (TypeNamed (Typeident name, args), copy_label label)

(* FIXME: should be removed *)
let row_label ty label : 'a row_t = (TyRow (encode_tuple [ty], None), copy_label label)
let row ty e = row_label ty (label e)

let tuple_type ?rowvar (l : _ ty list) : _ ty =
  let (tyr,label) = row_t_tuple ?rowvar l in
  (TypeRecord tyr, label)
let record_type ?rowvar (l : (string * _ ty) list) : _ ty =
  let (tyr,label) = row_t_record ?rowvar l in
  (TypeRecord tyr, label)
let tuple_string l = Printf.sprintf "tuple_%d" (List.length l)
let tuple_name l label =
  typenamed (tuple_string l) [] label



(** Utils on type definitions *)
let merge_type_def_visibility l pos =
  List.fold_left
    (fun accu vis ->
      match (accu, vis) with
      | (SurfaceAst.TDV_public, _) ->vis
      | (_, SurfaceAst.TDV_public) -> accu
      | (SurfaceAst.TDV_private, SurfaceAst.TDV_private) ->
          SurfaceAst.TDV_private
      | (SurfaceAst.TDV_abstract, SurfaceAst.TDV_abstract) ->
          SurfaceAst.TDV_abstract
      | (_, _) -> error_conflicting_type_def_visibility (accu, vis, pos))
    SurfaceAst.TDV_public
    l

let merge_type_def_options ~global ~local =
  { local with Q.
      opacapi = local.Q.opacapi || global.Q.opacapi ;
  }


(*
* Utils on built in type const
*)
(* should be factorized with the one in surfaceAstConvert *)
let type_const t label = (TypeConst t, copy_label label)
let tyint label = type_const TyInt label
let tyfloat label = type_const TyFloat label
let tystring label = type_const TyString label
let tyvoid_ = TypeRecord (TyRow ([], None))
let tyvoid label = (tyvoid_, copy_label label)



(*
* Utils on type vars
*)
let tyvar s label = ((TypeVar (Flatvar s)), copy_label label)
let fresh_tyvar label = tyvar (fresh_variable ()) label
let colvar s label = (SumVar (Colvar s), label)
let alpha label = tyvar "a" label
let beta label = tyvar "b" label




(*
* useful types (for directives at least)
*)
let tyxhtml label = typenamed Opacapi.Types.xhtml [] label
let tyxml label = typenamed Opacapi.Types.xml [] label
let tytext label = typenamed Opacapi.Types.text [] label
let tybool label = typenamed Opacapi.Types.bool [] label
let tylist ty label = typenamed Opacapi.Types.list [ty] label
let tyaction label = typenamed Opacapi.Types.Dom.transformation [] label
let tyopaty label = typenamed Opacapi.Types.OpaType.ty [] label
let tyoption label ty = typenamed Opacapi.Types.option [ty] label
let tycontinuation label ty = typenamed Opacapi.Types.continuation [ty] label

(*
* Utils on type directives
*)

let coerce e ty =
  Directive (`coerce, [e], [ty])
let coerce_expr e ty =
  (coerce e ty, nlabel e)
let coerce_pat p ty =
  (PatCoerce (p,ty), nlabel p)
let may_coerce_expr e o = Option.default_map e (coerce_expr e) o
let may_coerce_pat e o = Option.default_map e (coerce_pat e) o
let coerce_name e name =
  coerce e (typenamed name [] (label e))
let coerce_name_pat p name =
  coerce_pat p (typenamed name [] (label p))
let coerce_name_expr e name =
  (coerce_name e name, nlabel e)

(*
* Utils on constants
*)

let void_pat label = coerce_pat (PatRecord ([], `closed), copy_label label) (tyvoid label)
let void label = coerce_expr (Record [], copy_label label) (tyvoid label)
let true_pat label = PatRecord ([("true", void_pat label)], `closed), copy_label label
let true_ label = (Record [("true", void label)], copy_label label)
let false_pat label = PatRecord ([("false", void_pat label)], `closed), copy_label label
let false_ label = (Record [("false", void label)], copy_label label)
let string s label = (Const (CString s), copy_label label)
let string2 p = unc2 string p
let float2 (f,label) = (Const (CFloat f), copy_label label)
let float = cur2 float2
let int2 (i,label) = (Const (CInt (Big_int.big_int_of_int i)), copy_label label)
let int = cur2 int2
let floatint2 (i,label) = float2 (float_of_int i, label)
let intfloat2 (f,label) = int2 (int_of_float f, label)
let bool b label = if b then true_ label else false_ label

(*
* More Utils on directives
*)

let directive v le = Directive (v, le, [])
let directive0 v () = directive v []
let directive1 v e = directive v [e]
let directive1' v e = directive (v e) []
let directive2 v (e1, e2) = directive v [e1; e2]
let directive2' v (e1, e2) = directive (v e1) [e2]

let one_declaration_directive (((dir:parsing_directive),l,lt), label) bindings =
  List.map
    (fun (p,e) ->
       let args =
         match dir with
         | `specialize _ ->
             (*
This directive likes to have its arguments after the final expresssion.
*)
             e::l
         | _ -> l @ [ e ]
       in
       let e = (Directive (dir, args, lt), copy_label label) in
       (p,e)) bindings
let declaration_directive dirs bindings =
  List.fold_right one_declaration_directive dirs bindings


(*
* Utils on functions
*)

(* get the ident or field in a general call (ident, call dot composition) *)
let rec get_meaningful_called_ident (e,_) = match e with
  | Ident(id) -> id
  | Dot (_,field) -> field
  | Apply(f,_) -> get_meaningful_called_ident f
  | _ -> failwith "You are using autobind (i.e. ~) in an ambiguous way"


(* takes (arg1,arg2,...) and e and makes fun(arg1,arg2,...) -> e*)
let args_expr_to_lambda ?zero_ary args e =
  if args = [] then
    match zero_ary with
    | None -> e
    | Some label -> (Lambda ([], e), label)
  else
    let new_args = encode_args_as_record_pos args in
      (Lambda (undecorate new_args, e), union_annot2 new_args e)

(* puts a pos around the result *)
let (&) f args =
  if args = [] then
    f
  else
    let new_args = encode_args_as_record_pos args in
      (Apply (f, new_args), union_annot2 f new_args)

(* doesn't put a pos around the result *)
let (&.) f args =
  if args = [] then
    undecorate f
  else
    let new_args = encode_args_as_record_pos args in
      Apply (f, new_args)

let apply_f_with_holes f args : (_,_) expr =
  let args2,vars =
    let rec aux args vars = function
      | [] -> List.rev args, List.rev vars
      | h :: t ->
          match h with
            | `hole p ->
                let ident,patvar = fresh_ident_pat p in
                  aux (ident :: args) (patvar :: vars) t
            | `expr e ->
                aux (e :: args) vars t
    in
      aux [] [] args
  in
    if vars = [] then
      f & args2
    else
      args_expr_to_lambda vars (f & args2)

(* encode the 'function' construct of caml in opa AST *)
let function_ (l:(string SurfaceAst.pat * (string, 'a) SurfaceAst.expr) list) label =
  let var_expr,var_pat = fresh_ident_pat label in
    Lambda (encode_tuple [var_pat], (Match(var_expr, l), copy_label label))

let lambda_to_lambda (f: (_,_) expr -> (_,_) expr) label : (_,_) expr =
  let expr,pat = fresh_ident_pat label in
    args_expr_to_lambda [pat] (f expr)

let lambda s e =
  let p = var_to_patvar (s,label e) in
    Lambda (encode_tuple [p], e)

let rec dont_alias = function
  | (Ident _, _)
  | (Const _, _) -> true
  | (Dot (e,_), _) -> dont_alias e
  | _ -> false

(* takes a list of `expr expr or hole and gives you a list of expr and the
* record corresponding to the fresh arguments
* given [`expr 2, `hole], it gives you [2,x] and 1-uple (x,)
*)
let hole_processing l =
  let rec aux acc1 acc2 acc3 = function
    | [] -> List.rev acc1, acc2, acc3 (* not reversed because it is will be concatenated in another accumulator *)
    | h :: t ->
        match h with
          | `expr e ->
              if dont_alias e then
                aux (e :: acc1) acc2 acc3 t
              else
                let n = fresh_name () in
                let ident = var_to_exprvar (n,label e) in
                aux (ident :: acc1) acc2 ((n,e) :: acc3) t
          | `hole p ->
              let (ident,patvar) = fresh_ident_pat p in
                aux (ident :: acc1) (patvar :: acc2) acc3 t in
    aux [] [] [] l

let global_hole_processing l =
  let rec aux acc1 acc2 acc3 = function
    | [] -> List.rev acc1, List.rev acc2, List.rev acc3
    | (h,pos) :: t ->
        match h with
          | `dot _
          | `double_dot _ as v -> aux ((v,pos) :: acc1) acc2 acc3 t
          | `function_call args ->
              let exprs,vars,bindings = hole_processing args in
                aux ((`expr exprs,pos) :: acc1) (vars @ acc2) (bindings @ acc3) t
  in
    aux [] [] [] l

let make_function letins double_dot vars e el bindings =
  let body =
    List.fold_left
      (fun acc -> function
         | (`dot i,pos) -> (Dot(acc,i), pos)
         | (`double_dot i,pos) -> (undecorate (double_dot acc i), pos)
         | (`expr el,pos) -> Apply (acc, (encode_tuple el,pos)), pos) e el in
  let func = args_expr_to_lambda vars body in
    undecorate (letins bindings func)

(*
* applied to [f(_,1+2)], gives back:
* [ let fresh = 1 + 2 in
* fresh2 -> f(fresh2,fresh) ]
*)
let make_function2 letins double_dot e el =
  let exprs,vars,bindings =
    try
      List.map
        (fun e ->
           map_annot
             (function
                | `function_call a ->
                    `expr
                      (List.map
                         (function
                            | `hole _ -> raise Exit
                            | `expr v -> v
                         ) a)
                | `dot _ | `double_dot _ as v -> v) e) el, [], []
    with
      | Exit -> global_hole_processing el
  in
    match e with
      | `hole p ->
          let (ident,var) = fresh_ident_pat p in
          make_function letins double_dot (var :: vars) ident exprs bindings
      | `expr e ->
          make_function letins double_dot vars e exprs bindings
(*
* Utils on operators
*)

(* 1 + 2 + 3 -> parsed as -> (1,[+,2],[+,3] -> this function -> the correct tree of application *)
let extract_e = function
  | `expr e -> e
  | `hole p -> failwith (sprintf "At %s: `_' is not a valid expression: it has to be a direct argument of a function/operator" (FilePos.to_string p.QmlLoc.pos)) (* the parser only accepts holes when they are not alone, so this should not occur *)
let apply_operators associativity ((e1 : [ `expr of (string, 'a) SurfaceAst.expr | `hole of QmlLoc.annot ]),l) =
  match l with
    | [] -> extract_e e1
    | _ ->
        match associativity with
          | `nonassoc ->
              ( match l with
                  | [(op,e2)] -> apply_f_with_holes op [e1;e2]
                  | _ -> failwith "You are using a non associative operator in an ambiguous way" (* FIXME: should be understandable, but unused for now *)
              )
          | `left ->
              extract_e (List.fold_left (fun acc (op,e) -> `expr (apply_f_with_holes op [acc;e])) e1 l)
          | `right ->
              let (ops,es) = List.split l in
              let (new_es,new_e1) = List.extract_last (e1 :: es) in
              let new_l = List.combine ops new_es in
                extract_e (List.fold_right (fun (op,e) acc -> `expr (apply_f_with_holes op [e;acc])) new_l new_e1)




(*
* Utils on datatype
*)

let tuple_nocons l =
  let (l2,label) = encode_tuple_pos l in
  l2, copy_label label

let tuple l =
  let r = tuple_nocons l in
  Record (undecorate r), nlabel r

let record_nocons l =
  let (l2,label) = encode_record_pos l in
  l2, copy_label label

let record l =
  let r = record_nocons l in
  Record (undecorate r), nlabel r

let tuple_pat_nocons l =
  let (l2,label) = encode_tuple_pos l in
  l2, copy_label label

let tuple_pat l =
  let r = tuple_pat_nocons l in
  PatRecord (undecorate r, `closed), nlabel r

let record_pat_nocons l =
  let (l2,label) = encode_record_pos l in
  l2, copy_label label

let record_pat l =
  let r = record_pat_nocons l in
  PatRecord (undecorate r, `closed), nlabel r

let simple_record s label = Record [(s, void label)]
let record1 s e = (Record [(s,e)], nlabel e)
let simple_record_expr s label = wrap (simple_record s) label
let simple_record_expr2 = unc2 simple_record_expr

(*let list_pat_of_pat_list l label = List.fold_right list_cons_pat l (list_nil_pat label)*)

let some e = coerce_name_expr (record1 "some" e) Opacapi.Types.option
let none label = coerce_name_expr (simple_record_expr "none" label) Opacapi.Types.option
let option_expr_of_expr_option o label =
  match o with
    | None -> none label
    | Some e -> some e

(*
* Utils on constructs
*)
let letins iel e =
  if iel = [] then e else (LetIn (false,iel,e), union_annot (union_annot_list_snd iel) (label e))
let letin i e1 e2 = letins [(i,e1)] e2
(* could do: let letinpat : pat -> expr -> expr -> expr qui fait "let pat = expr in expr" *)
let dot e s = (Dot (e,s), nlabel e)
let dot_path e path =
  List.fold_left dot e path
let dots l label =
  match l with
    | [] -> assert false
    | h :: t ->
        List.fold_left (fun acc s -> (Dot (acc,s), copy_label label)) (ident h (copy_label label)) t
let dots2 l =
  match l with
    | [] -> assert false
    | h :: t ->
        List.fold_left (fun acc (s2,pos2) -> (Dot (acc,s2), pos2)) (unc2 ident h) t
(* FIXME: redondant with (&) *)
let applys e el =
  match el with
    | [] -> e
    | _ -> let r = tuple_nocons el in (Apply (e, r), union_annot2 e r)
let apply e1 e2 = applys e1 [e2]


(*
* Functions that are expected to be defined later
*)

let append e1 e2 = ident "++" (nlabel e1) & [e1;e2]
let stringmap_empty label = dots ["StringMap";"empty"] label
let cssentrymap_empty label = dots ["Css_private";"Entry_map_empty"] label
let stringmap_add pos k v map = dots ["StringMap";"add"] pos & [k;v;map]
let cssentrymap_add pos k v map = dots ["Css_private";"Entry_map_add"] pos & [k;v;map]
let map_add_merge pos k data map = ident Opacapi.internal__add_css_entry pos & [k;data;map]
let db_write e1 e2 =
  let ident = var_to_exprvar ("<-",label e2) in
  Apply (ident, encode_tuple_pos [e1;e2])

(*
* Utils on patterns
*)

let if_then_else e1 e2 o =
  let e3 = Option.default (void (label e2)) o in
    Match (e1, [(true_pat (label e2), e2);
                   (false_pat (label e3), e3);
               ])

let type_of_record label r =
  Cons.T.record ~label (List.map (fun (s,_) -> (s, fresh_tyvar label)) r)

let rec bind name acc = function
  | (PatConst _,label) as p ->
      `one (fresh_name (), Cons.E.match_ ~label (Cons.E.ident ~label name) [(p,Cons.E.void ~label ())]) :: acc
  | (PatAny,_) -> acc
  | (PatVar v,label) -> `one (v.ident, Cons.E.ident ~label name) :: acc
  | PatRecord (r, rowvar), label ->
      bind_aux_record label name acc rowvar r
  | (PatAs (p,v),label) -> bind name (`one (v.ident,Cons.E.ident ~label name) :: acc) p
  | (PatCoerce (p,ty),label) -> bind name (`one (fresh_name (), Cons.E.coerce ~label (Cons.E.ident ~label name) ty) :: acc) p
and bind_aux_record label name acc rowvar r =
  let bindings, block =
    List.fold_left_map (fun acc (s,p) ->
                      let n,p =
                        match p with
                          | (PatAs (p,s),_) -> s.ident,p
                          | (PatVar s,label) -> s.ident, Cons.P.any ~label ()
                          | _ -> fresh_name (), p in
                      let label = snd p in
                      bind n acc p, (n, Cons.E.dot ~label (Cons.E.ident ~label name) s)
                   ) [] r in
  let bindings = bindings @ (`list block :: acc) in
  if rowvar = `closed then
    (* coercions to make sure we don't have additional fields
* [match x with ~{a} -> y] -> [a = x.a; y]
* would work with x = {a b} when it shouldn't
*)
    let typ = type_of_record label r in
    let n = fresh_name () in
    `one (n, Cons.E.coerce ~label (Cons.E.ident ~label name) typ) :: bindings
  else
    bindings

let create_letins ~label dirs l e2 =
  List.fold_right
    (fun v acc ->
       match v with
       | `one (s,e1) -> (LetIn (false, declaration_directive dirs [s,e1], acc), copy_label label)
       | `list l -> (LetIn (false, declaration_directive dirs l, acc), copy_label label)
    ) l e2

(* transforms [let (a,b) = e1 in e2] in
* let fresh = e1 in
* let a = fresh.f1 in
* let b = fresh.f2 in
* e2
*)
let rec bind_in_to_expr_in dirs binding e2 =
  let (p,e1) = binding in
  undecorate (
    match p with
      | (PatVar v, label) ->
        assert( v.directives = [] );
        (LetIn (false,declaration_directive dirs [(v.ident,e1)],e2),copy_label label)
      | (PatAny, label) -> (LetIn (false,declaration_directive dirs [(fresh_name (),e1)],e2),copy_label label)
      | (PatCoerce (p,ty),label) -> (bind_in_to_expr_in dirs (p,Cons.E.coerce ~label e1 ty) e2,label)
      | (_,label) ->
          let n = fresh_name () in
          let bindings = `one (n,e1) :: List.rev (bind n [] p) in
          create_letins ~label dirs bindings e2
  )

let add_recval ~is_recval label (i,e) =
  if is_recval then
    (* avoid putting two @recval on the same binding *)
    let rec aux e =
      match e with
      | (Directive (`coerce, [e], [ty]), label) ->
          (Directive (`coerce, [aux e], [ty]), label)
      | (Directive (`recval, _, _),_) -> e
      | _ -> (Directive (`recval, [e], []), copy_label label) in
    (i, aux e)
  else
    (i,e)
let rec pat_in_to_simple_bindings_aux ~is_recval (p,e) =
  match p with
  | (PatVar v, _) -> assert( v.directives = [] );[(v.ident,e)]
  | (PatAny, _) -> [(fresh_name (),e)]
  | (PatCoerce (p,ty),label) -> pat_in_to_simple_bindings_aux ~is_recval (p,Cons.E.coerce ~label e ty)
  | (_, label) ->
      let n = fresh_name () in
      let bindings = `one (n,e) :: List.rev (bind n [] p) in
      List.concat_map
        (function
         | `one b -> [add_recval ~is_recval label b]
         | `list l -> List.map (add_recval ~is_recval label) l) bindings
let pat_in_to_simple_bindings (p,e) =
  let is_recval =
    match e with
    | (Directive (`recval, [_], []),_) -> true
    | _ -> false in
  pat_in_to_simple_bindings_aux ~is_recval (p,e)

let module_ e = directive1 `module_ e
let parser_ parser_ = directive0 (`parser_ parser_) ()
let open1 e1 e2 = directive2 `open_ (e1, e2)
let open1_pos e1 e2 =
  (open1 e1 e2, nlabel e1)
let open_ el e =
  List.fold_right open1_pos el e
let xml_parser xml_parser =
  directive0 (`xml_parser xml_parser) ()
let magic_to_string e =
  directive1 `magic_to_string e
let magic_to_xml e =
  directive1 `magic_to_xml e
let magic_to_text e =
  directive1 `magic_to_text e
let fun_action e =
  directive1 `fun_action e
let magic_do e = (directive1 `magic_do e , nlabel e)
let computed_string l = directive `string l

(*
* stuff on list put here because they need coerce_name_expr
*)
let list_nil label = coerce_name_expr (record [("nil",void label)]) Opacapi.Types.list
let list_nil_no_coerce label = (record [("nil",void label)])
let list_nil_pat label = coerce_name_pat (record_pat [("nil",void_pat label)]) Opacapi.Types.list
let list_cons e1 e2 = coerce_name_expr (record [("hd",e1);("tl",e2)]) Opacapi.Types.list
let list_cons_no_coerce e1 e2 = (record [("hd",e1);("tl",e2)])
let list_cons_pat e1 e2 = coerce_name_pat (record_pat [("hd",e1);("tl",e2)]) Opacapi.Types.list
let list_pat_of_pat_list ?tl l label : string pat =
  let tl = match tl with None -> list_nil_pat label | Some tl -> tl in
  List.fold_right list_cons_pat l tl
let list_expr_of_expr_list ?tl l label =
  let tl = match tl with None -> list_nil label | Some tl -> tl in
  List.fold_right list_cons l tl
let list_expr_of_expr_list_no_coerce ?tl l label =
  let tl = match tl with None -> list_nil_no_coerce label | Some tl -> tl in
  List.fold_right list_cons_no_coerce l tl
let list_expr_of_expr_list_unsafe l =
  assert (l <> []);
  let pos = union_annot_list l in list_expr_of_expr_list l pos
(*
* same as above with record stuff
*)
let default_value_in_expr_record tilda f =
  let set_default =
    if tilda then
      fun i -> var_to_exprvar i
    else
      fun i -> void (label i) in
  List.map (function
              | `binding b -> b
              | `noassign (i, `value p, t) ->
                  (i,may_coerce_expr p t)
              | `noassign (i, `novalue i2, t) ->
                  (i, may_coerce_expr (set_default i2) t)) f
let default_value_in_pat_record tilda f =
  let default_value =
    if tilda then
      fun i -> var_to_patvar i
    else
      fun i -> void_pat (label i) in
  List.map (fun (ident,p,t) ->
              let p = match p with
                | `value p -> p
                | `novalue i -> default_value i in
              (ident, may_coerce_pat p t)) f
let default_value_in_type_record tilda f =
  let default_value =
    if tilda then
      fun i -> typenamed (undecorate i) [] (label i)
    else
      fun i -> tyvoid (label i) in
  List.map (fun (ident,p) ->
              let p = match p with
                | `value p -> p
                | `novalue i -> default_value i in
              (ident, p)) f

let list_constructors_of_string_list l lab =
  match l with
  | [] -> list_nil lab
  | _ -> list_expr_of_expr_list_unsafe (List.map simple_record_expr2 l)


(*
* Utils for the toplevels
*)
(* since we don't have directive for the toplevel,
* open at toplevel is encoded as _ = @toplevel_open(e) *)
let toplevel_open e =
  NewVal ([((PatAny, nlabel e), (Directive (`toplevel_open, [e], []), nlabel e))],false)
let toplevel_opens el =
  List.map toplevel_open el

(*
* Others
*)

let bypass s = Bypass (BslKey.normalize s)
(*
* [e..i] is transformed into [fresh = e
fresh.i(fresh)]
*)
let double_dot e i =
  let name = fresh_name () in
  let fresh_e = var_to_exprvar (name, nlabel e) in
    letin name e (apply (dot fresh_e i) fresh_e)

(*
* actions
*)

let action _filename jqs val_css verb e : (_,_) expr_node =
  let a,f,others = undecorate val_css in
  let tl = Option.default [] others in
  let f =
    match f with
      | `identity -> (fun e -> e)
      | `magicToString -> (fun e -> (magic_to_string e, label val_css))
      | `magicToXml -> (fun e -> (magic_to_xml e, label val_css)) in
  let verb_s,verb_label = verb in
    if a = "css" && verb_s <> "set" then
      OManager.error "In file %s, there is an invalid operation (%s) for CSS. you can only set CSS." _filename verb_s;
    let record =
      record
        [("jq", jqs);
         ("subject", coerce_name_expr (record ((a, f e) :: tl)) Opacapi.Types.Dom.Transformation.subject);
         ("verb", ((simple_record verb_s verb_label), verb_label))]
    in
      coerce_name record Opacapi.Types.Dom.transformation

let action_verb verb =
  let build s = simple_record_expr s (snd verb) in
  match fst verb with
  | `set -> build "set"
  | `prepend -> build "prepend"
  | `append -> build "append"

let action_content selector verb expr =
  let subject =
    let expr = (magic_to_xml expr, label expr) in
    record1 "content" expr
  in
  let verb = action_verb verb in
  record [("jq", selector); ("subject", subject); ("verb", verb)]

let action_css selector verb expr =
  let subject = record1 "css" expr in
  let verb = action_verb verb in
  record [("jq", selector); ("subject", subject); ("verb", verb)]

(*
* xml
*)

(**
Reset default namespace to that of xhtml
*)
let around_xhtml expr =
  letin "xmlns:" (dot (ident "Xhtml" (nlabel expr)) "ns_uri") expr

(**
Reset default namespace to that of xmlns, i.e. no namespace
*)
let around_xmlns expr =
  letin "xmlns:" (dot (ident "Xmlns" (nlabel expr)) "default_ns_uri") expr

type xml_or_xhtml =
  | Xml
  | Xhtml
let xml_stack = Stack.create ()
let push_xml x = Stack.push x xml_stack
let pop_xml () = ignore (Stack.pop xml_stack)
let xhtml_mode () = Stack.top xml_stack = Xhtml
let xml_typename () = if xhtml_mode () then Opacapi.Types.xhtml else Opacapi.Types.xml

let tag_stack = Stack.create ()
let push_tag s = Stack.push s tag_stack
let get_tag() = Stack.top tag_stack
let pop_tag() = ignore (Stack.pop tag_stack)

type 'b dom_tag_args = {
  (* Non-special attributes *)
  args : (string * string * (string, 'b) expr) list;

  (* Class names -- why is it special? *)
  class_ : (string, 'b) expr option;

  (* CSS styles -- special probably because we want to preprocess them *)
  style : (string, 'b) expr option;

  (* Namespace bindings of the form [xmlns:bar = "foo"] or [xmlns = "foo"] *)
  xmlns_declaration: (string(*prefix, possibly empty*) * (string, 'b) expr(*unique uri*)) list;

  events : ((string, 'b) expr) list;
  events_expr : (string, 'b) expr option;
  events_options: ((string, 'b) expr) list;
  href: (string, 'b) expr option
} constraint 'b = [> `coerce ]

(* const -> string s label *)
(* list_append_opa -> append e1 e2 *)

let appendlo l o =
  match o with
    | None -> l
    | Some l2 -> append l l2
(* represents [CSS_build.v(args)] *)
let css_build v args : (_,_) expr =
  dot (ident "Css_build" (label v)) (undecorate v) & args
let css_build_unsafe s args : (_,_) expr =
  match args with
    | [] -> assert false
    | h :: _ -> css_build (s, nlabel h) args
let css_build1 s pos = css_build (s,pos) []
let css_build1' s e = css_build (s,nlabel e) []

let hyphen_to_underscore s = String.replace s "-" "_"
let map_tuple4 f (a,b,c,d) = (f a, f b, f c, f d)



let to_handle (name : string * QmlLoc.annot) : (_,_) expr =
  coerce_name_expr (record [(undecorate name, void (label name))]) Opacapi.Types.Dom.Event.kind

let hassoc name value =
  coerce_name_expr (record [("name", to_handle name); ("value", value)]) Opacapi.Types.handle_assoc

let hassoc_event name value =
  coerce_name_expr (hassoc name value) Opacapi.Types.event_handler

let empty_args _label = { args = [];
                         class_ = None;
                         style = None;
                         events = [];
                         events_options = [];
                         events_expr = None;
                         xmlns_declaration = [];
                         href = None
                       }

let is_empty_args {args=_; class_; style; events; events_options; events_expr; xmlns_declaration=_; href} =
  events = [] && events_options = [] && events_expr = None && href = None &&
  class_ = None && style = None

let arg_default (o,label) =
  match o with
    | None -> empty_args label
    | Some o -> o

let create_fragment l label =
  match l with
    | [h] -> h
    | l -> coerce_name_expr (record [("fragment", list_expr_of_expr_list l label)]) (xml_typename ())

let create_textnode str =
  coerce_name_expr (record [("text", string2 str)]) (xml_typename ())

(* if we have some spaces, create a textnode, except if we are at the end *)
let create_fragment_with_spaces (l,label) =
  let aux (o,n) =
    match o with
      | Some p -> [create_textnode p; n]
      | None -> [n] in
  let l2 = BaseList.concat_map aux l in
  match l2 with
  | [] -> assert false
  | [h] -> h
  | _ -> create_fragment l2 label

let create_empty_fragment label = create_fragment [] label

let empty_event_handler label =
  record ["value", (string "" label)]
(* args_expr_to_lambda [PatAny, label] (void label)*)

let sassoc (ns,name) (value:(_,_) expr) : (_,_) expr =
  coerce_name_expr (record [("namespace", unc2 ident ns);
                            ("name", unc2 string name);
                            ("value", value)]) Opacapi.Types.Xml.attribute

let create_element (ns,tag) args children =
  (* Adapt tag and attributes *)
  let tag_ns : (string, 'a) SurfaceAst.expr =
    let (name, annot) = ns in
      ident ("xmlns:"^name) annot in (* Note: [name] can be empty, it's ok *)
  let other_attributes = List.map (fun (prefix, name, value) -> sassoc (("xmlns:"^prefix, nlabel tag), (name, nlabel tag)) value) args.args in

  (* Create element *)
  let record =
    if xhtml_mode () && not (is_empty_args args) then (
      let events = list_expr_of_expr_list args.events (nlabel tag)
      and events_options = list_expr_of_expr_list args.events_options (nlabel tag) in
      let class_ =
        match args.class_ with
        | None -> list_nil (label ns)
        | Some c -> c in
      let style =
        match args.style with
        | None -> list_nil (label ns)
        | Some s -> s in
      let specific_attributes =
        record [("class",class_);
                ("style",style);
                ("events",appendlo events args.events_expr);
                ("events_options", events_options);
                ("href", match args.href with
                 | None -> coerce_name_expr (simple_record_expr "none" (nlabel tag)) Opacapi.Types.xhtml_href
                 | Some s -> s)
               ] in
      record [("namespace",tag_ns);
              ("tag",unc2 string tag);
              ("args", list_expr_of_expr_list other_attributes (label tag));
              ("specific_attributes", some specific_attributes);
              ("content",list_expr_of_expr_list children (label tag));
             ]
    ) else (
      record [("namespace",tag_ns);
              ("tag",unc2 string tag);
              ("args", list_expr_of_expr_list other_attributes (label tag));
              ("content",list_expr_of_expr_list children (label tag));
              ("specific_attributes", none (label tag));
             ]
    ) in


  (* Declare namespaces.
Declaration [xmlns = "foo"] is converted into a binding <<`xmlns:` = foo>. *)
  let record = List.fold_left
    (fun record (prefix, uri) -> letin ("xmlns:"^prefix) uri record) record
    args.xmlns_declaration in

  coerce_name_expr record (xml_typename ())

let nstag_to_string ns tag =
    if ns = "" then tag else (sprintf "%s:%s" ns tag)

(* we allow any tags to be closed by </> thus, there is no error if tag2 = "" *)
let tag_mismatch ((ns1,_annot), (tag1,{QmlLoc.pos = pos1})) ((ns2,_annot), (tag2,{QmlLoc.pos = _pos2})) =
  let matched = (tag2 = "") || (tag1 = tag2 && ns1 = ns2) in
    if not matched then
      error1 (sprintf "and %s\n Open and close tag mismatch <%s> vs </%s>"
                (FilePos.to_string pos1)
                (nstag_to_string ns1 tag1)
                (nstag_to_string ns2 tag2))
        _annot
    else ()

let add_arg src ((prefix,_),name) value =
  let old = arg_default src in
  { old with args = (prefix, fst name, Option.default (unc2 string name) value) :: old.args }




let add_event src name value =
  let name = match name with (s,pos) -> (String.lowercase s, pos) in
  let old = arg_default src in
    { old with events = (hassoc_event name value) :: old.events }
let add_event_option src name value =
  let name = match name with (s,pos) -> (String.lowercase s, pos) in
  let old = arg_default src in
    { old with events_options = (hassoc name value) :: old.events_options }
let add_events src value =
  let old = arg_default src in
    { old with events_expr = Some value }
let action_xml e =
  let e = coerce_name_expr e Opacapi.Types.FunAction.t in
  let e = wrap_e fun_action e in
  coerce_name_expr (record [("expr", e)]) Opacapi.Types.xhtml_event

let concat_xml_text l =
  let string = String.concat "" l in
  let buf = Buffer.create (String.length string) in
  let _ = String.fold (fun cleaning c ->
      if cleaning then
        if Char.is_space c then true (*Discard char, carry on cleaning*)
        else
          begin
            Buffer.add_char buf c; (*Keep the char, stop cleaning*)
            false
          end
      else
        begin
          Buffer.add_char buf c; (*Keep char, no matter what*)
          c = '\n' || c = '\r' (*If this is a newline, start cleaning*)
        end
  ) false string in
  Buffer.contents buf

(*
* Css
*)
let hexa2 (a,pos) (b,_) = int2 ((Char.hexa_value a) * 16 + Char.hexa_value b, pos)
let remove_percent = function
  | (Record [("percent",(Const (CInt i),_label2))], label) ->
      let i = Big_int.div_big_int (Big_int.mult_int_big_int 255 i) (Big_int.big_int_of_int 100) in
      (Const (CInt i), label)
  | e -> e
let rgba r g b a = record [("r", remove_percent r);("g", remove_percent g);("b", remove_percent b);("a", remove_percent a)]
let color_hexa a b c d e f g h =
  let r = hexa2 a b
  and g = hexa2 c d
  and b = hexa2 e f
  and a = hexa2 g h in
    rgba r g b a
let rgb r g b = rgba r g b (int2 (255,label b))

(*
* inlined css
*)

let conv (v,pos) =
  match v with
    | `left -> simple_record_expr "left" pos
    | `right -> simple_record_expr "right" pos
    | `center -> simple_record_expr "center" pos
    | `top -> simple_record_expr "top" pos
    | `bottom -> simple_record_expr "bottom" pos
    | `size s -> record1 "size" s

let background_position ((f,posf) as l) s =
  match (f,s) with
    | (`left|`right|`center|`size _), None ->
        conv l, conv (`center,posf)
    | (`top|`bottom), None ->
        conv (`center,posf), conv l
    | (`size _), Some ((`size _,_) as v) ->
        conv l, conv v
    | (`size _), Some v ->
        conv l, conv v
    | _, Some ((`size _,_) as v) ->
        conv l, conv v
    | _, Some ((c,_) as r) ->
        if List.mem f [`left;`right] || List.mem c [`top;`bottom] then
          conv l, conv r
        else
          conv r, conv l
let hyphen_to_underscore s = String.replace s "-" "_"
let css_build_with_prefix prefix (s,pos) = css_build1 (prefix^s) pos
let simple_record_hyphen2 (s,pos) =
  simple_record_expr (hyphen_to_underscore s) pos
let css_simple_record_hyphen2 (s,pos) =
  simple_record_expr (hyphen_to_underscore (if s = "none" then "css_none" else s)) pos

let list_style_def (s,pos) =
  match s with
    | "disc" | "square" -> simple_record_expr s pos
    | "decimal-leading-zero" -> record1 "decimal" (simple_record_expr "leading" pos)
    | "decimal" -> record1 "decimal" (simple_record_expr "not_leading" pos)
    | "lower-roman" -> record1 "roman" (simple_record_expr "lower_case" pos)
    | "upper-roman" -> record1 "roman" (simple_record_expr "upper_case" pos)
    | "lower-alpha" | "lower-latin" -> record1 "latin" (simple_record_expr "lower_case" pos)
    | "upper-alpha" | "upper-latin" -> record1 "latin" (simple_record_expr "upper_case" pos)
    | "lower-greek" -> simple_record_expr "greek" pos
    | _ -> assert false

(*---------------------------------*)
(*------- deep record update ------*)
(*---------------------------------*)

let find_and_remove p l =
  let rec aux acc = function
    | [] -> raise Not_found
    | h :: t ->
      if p h then h, List.rev_append acc t
      else aux (h :: acc) t in
  aux [] l

type tree = (string * QmlLoc.annot) * tree_aux
and tree_aux =
  | Leaf of (string, parsing_directive) expr
  | Node of tree list

let has_name ((s,_):(string*QmlLoc.annot)) (((s',_),_):tree) = s = s'

let make_path (sl,e) =
  let rec aux = function
    | [] -> assert false
    | [s] -> (s, Leaf e)
    | s :: sl -> (s, Node [aux sl]) in
  aux sl

let pp_pos_annotated f (s:(string * QmlLoc.annot)) =
  FilePos.pp_pos f ((snd s).QmlLoc.pos)

let add_path trees ((sl:(string * QmlLoc.annot) list),e) =
  let rec aux ~error_s (s,t_aux) sl =
    match t_aux with
    | Leaf _ ->
      OManager.serror "@[<v>%a@]@\n@[<2> This record update tries to redefine a path.@\n(at %a)@]" pp_pos_annotated error_s pp_pos_annotated s;
      (match sl with
      | [] -> []
      | _ -> make_path (sl,e) :: [])
    | Node trees ->
      match sl with
      | [] ->
        OManager.serror "@[<v>%a@]@\n@[<2> This record update tries to overwrite a path previously defined.@\n(at %a)@]" pp_pos_annotated error_s pp_pos_annotated s;
        trees
      | s :: sl ->
        auxs trees s sl
  and auxs trees s sl =
    try let tree, trees = find_and_remove (has_name s) trees in
        (s, Node (aux ~error_s:s tree sl)) :: trees
    with Not_found ->
      make_path (s :: sl,e) :: trees in
  match sl with
  | [] -> assert false
  | s :: sl ->
    auxs trees s sl

let make_record_tree (l : ((string * QmlLoc.annot) list * (_,_) expr) list) =
  List.fold_left add_path [] l

let rec rewrite_record_update_aux e (((s,annot),t_aux):tree) =
  match t_aux with
  | Leaf v -> v
  | Node ts -> rewrite_record_update (Dot (e,s), annot) ts
and rewrite_record_update (e:(_,_) expr) (ts:tree list) =
  let path, wrapper =
    match e with
    | (Ident _,_) -> e, (fun x -> x)
    | _ ->
      if List.for_all (function (_,Node _) -> false | _ -> true) ts then
        e, (fun x -> x)
      else
        let i = fresh_name () in
        (Ident i, nlabel e), (fun body -> (LetIn (false, [i, e], body), nlabel e)) in
  wrapper (
    (ExtendRecord (
       List.rev_map (fun (((s,_),_) as t) -> (s, rewrite_record_update_aux path t)) ts,
       path
     ),
     nlabel e
    )
  )

let rewrite_long_extend_record fields expr =
  let trees = make_record_tree fields in
  rewrite_record_update expr trees

(*
* Functions that are expected to be defined later
*)
let dom_transform l =
  let ident = var_to_exprvar ("Dom_transform", nlabel l) in
  Apply(ident, encode_tuple_pos [l])

let binding_to_pattern_binding (ident, expr) =
  (var_to_patvar ident), expr
Something went wrong with that request. Please try again.