X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fcic_notation%2FcicNotationPres.ml;h=395cab6c29927e01cc1760ec1ac6ccc28e6fd87f;hb=cd602bc57c4ceba6188b4cac0dbf5dad8f5df7b6;hp=97659b763271be9e936a8d3240c4030a70f5fca5;hpb=d25c0d1fd0dee1036df6f3bbc62c75c55c3bd314;p=helm.git diff --git a/helm/ocaml/cic_notation/cicNotationPres.ml b/helm/ocaml/cic_notation/cicNotationPres.ml index 97659b763..395cab6c2 100644 --- a/helm/ocaml/cic_notation/cicNotationPres.ml +++ b/helm/ocaml/cic_notation/cicNotationPres.ml @@ -31,10 +31,7 @@ type markup = mathml_markup let atop_attributes = [None, "linethickness", "0pt"] let binder_attributes = [None, "mathcolor", "blue"] let indent_attributes = [None, "indent", "1em"] - -let mpres_arrow = Mpresentation.Mo (binder_attributes, "->") - (* TODO unicode symbol "to" *) -let mpres_implicit = Mpresentation.Mtext ([], "?") +let keyword_attributes = [None, "mathcolor", "blue"] let to_unicode s = try @@ -67,32 +64,33 @@ let genuine_math = let box_of mathonly spec attrs children = match children with - | [t] -> t - | _ -> - let kind, spacing, indent = spec in - let rec dress = function - | [] -> [] - | [hd] -> [hd] - | hd :: tl -> hd :: Mpresentation.Mtext ([], " ") :: dress tl - in - if mathonly then Mpresentation.Mrow (attrs, dress children) - else - let attrs' = - if spacing then [None, "spacing", "0.5em"] else [] - @ if indent then [None, "indent", "0em 0.5em"] else [] - @ attrs - in - match kind with - | CicNotationPt.H when List.for_all genuine_math children -> - Mpresentation.Mrow (attrs', children) - | CicNotationPt.H -> - mpres_of_box (Box.H (attrs', List.map box_of_mpres children)) - | CicNotationPt.V -> - mpres_of_box (Box.V (attrs', List.map box_of_mpres children)) - | CicNotationPt.HV -> - mpres_of_box (Box.HV (attrs', List.map box_of_mpres children)) - | CicNotationPt.HOV -> - mpres_of_box (Box.HOV (attrs', List.map box_of_mpres children)) + | [t] -> t + | _ -> + let kind, spacing, indent = spec in + let dress children = + if spacing then + CicNotationUtil.dress (Mpresentation.Mtext ([], " ")) children + else + children + in + if mathonly then Mpresentation.Mrow (attrs, dress children) + else + let attrs' = + (if spacing then [None, "spacing", "0.5em"] else []) + @ (if indent then [None, "indent", "0em 0.5em"] else []) + @ attrs + in + match kind with + | CicNotationPt.H when List.for_all genuine_math children -> + Mpresentation.Mrow (attrs', dress children) + | CicNotationPt.H -> + mpres_of_box (Box.H (attrs', List.map box_of_mpres children)) + | CicNotationPt.V -> + mpres_of_box (Box.V (attrs', List.map box_of_mpres children)) + | CicNotationPt.HV -> + mpres_of_box (Box.HV (attrs', List.map box_of_mpres children)) + | CicNotationPt.HOV -> + mpres_of_box (Box.HOV (attrs', List.map box_of_mpres children)) let open_paren = Mpresentation.Mo ([], "(") let closed_paren = Mpresentation.Mo ([], ")") @@ -142,8 +140,8 @@ let is_atomic t = aux_mpres t let add_parens child_prec child_assoc child_pos curr_prec t = - prerr_endline (Printf.sprintf "add_parens %d %s %s %d" child_prec - (pp_assoc child_assoc) (pp_pos child_pos) (curr_prec)); +(* prerr_endline (Printf.sprintf "add_parens %d %s %s %d" child_prec + (pp_assoc child_assoc) (pp_pos child_pos) (curr_prec)); *) if is_atomic t then t else if child_prec < curr_prec || (child_prec = curr_prec && @@ -168,9 +166,18 @@ let render ids_to_uris = | None -> None | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None) in - let make_href xref = - let uri = lookup_uri xref in - make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri] + let make_href xmlattrs xref uris = + let xref_uri = lookup_uri xref in + let raw_uris = List.map UriManager.string_of_uri uris in + let uri = + match xref_uri, raw_uris with + | None, [] -> None + | Some uri, [] -> Some uri + | None, raw_uris -> Some (String.concat " " raw_uris) + | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris)) + in + xmlattrs + @ make_attributes [Some "helm", "xref"; Some "xlink", "href"] [xref; uri] in let make_xref xref = make_attributes [Some "helm","xref"] [xref] in let make_box = function @@ -179,38 +186,45 @@ let render ids_to_uris = box | m -> Box.Object ([], m) in - let make_hv xref children = - let attrs = indent_attributes @ make_href xref in - P.Mobject ([], Box.HV (indent_attributes, List.map make_box children)) - in (* when mathonly is true no boxes should be generated, only mrows *) - let rec aux mathonly xref pos prec t = + let rec aux xmlattrs mathonly xref pos prec uris t = match t with - | A.AttributedTerm (`Loc _, t) -> aux mathonly xref pos prec t - | A.AttributedTerm (`Level (child_prec, child_assoc), t) -> - let t' = aux mathonly xref pos child_prec t in - add_parens child_prec child_assoc pos prec t' - | A.AttributedTerm (`IdRef xref, t) -> aux mathonly (Some xref) pos prec t - | A.Ident (literal, _) -> P.Mi (make_href xref, to_unicode literal) - | A.Num (literal, _) -> P.Mn (make_href xref, to_unicode literal) - | A.Symbol (literal, _) -> P.Mo (make_href xref,to_unicode literal) - | A.Uri (literal, _) -> P.Mi (make_href xref, to_unicode literal) - | A.Literal l -> aux_literal xref prec l - | A.Layout l -> aux_layout mathonly xref pos prec l + | A.AttributedTerm (attr, t) -> + aux_attribute xmlattrs mathonly xref pos prec uris t attr + | A.Ident (literal, _) -> + P.Mi (make_href xmlattrs xref [], to_unicode literal) + | A.Num (literal, _) -> + P.Mn (make_href xmlattrs xref [], to_unicode literal) + | A.Symbol (literal, _) -> + P.Mo (make_href xmlattrs xref uris, to_unicode literal) + | A.Uri (literal, _) -> + P.Mi (make_href xmlattrs xref [], to_unicode literal) + | A.Literal l -> aux_literal xmlattrs xref prec uris l + | A.Layout l -> aux_layout mathonly xref pos prec uris l | A.Magic _ | A.Variable _ -> assert false (* should have been instantiated *) | t -> prerr_endline (CicNotationPp.pp_term t); assert false - and aux_literal xref prec l = - let attrs = make_href xref in + and aux_attribute xmlattrs mathonly xref pos prec uris t = + function + | `Loc _ + | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t + | `Level (child_prec, child_assoc) -> + let t' = aux xmlattrs mathonly xref pos child_prec uris t in + add_parens child_prec child_assoc pos prec t' + | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t + | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t + | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t + and aux_literal xmlattrs xref prec uris l = + let attrs = make_href xmlattrs xref uris in match l with - | `Symbol s + | `Symbol s -> P.Mo (attrs, to_unicode s) | `Keyword s -> P.Mo (attrs, to_unicode s) | `Number s -> P.Mn (attrs, to_unicode s) - and aux_layout mathonly xref pos prec l = + and aux_layout mathonly xref pos prec uris l = let attrs = make_xref xref in - let invoke' t = aux true None pos prec t in + let invoke' t = aux [] true None pos prec uris t in match l with | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2) | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2) @@ -222,40 +236,55 @@ let render ids_to_uris = P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2) | A.Sqrt t -> P.Msqrt (attrs, invoke' t) | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2) - | A.Box (kind, terms) -> - let children = aux_children mathonly xref pos prec terms in + | A.Box ((_, spacing, _) as kind, terms) -> + let children = aux_children mathonly spacing xref pos prec uris (CicNotationUtil.ungroup terms) in box_of mathonly kind attrs children - and aux_children mathonly xref pos prec terms = - let rec aux_list first = + | A.Group terms -> + let children = aux_children false mathonly xref pos prec uris (CicNotationUtil.ungroup terms) in + box_of mathonly (A.H, false, false) attrs children + | A.Break -> assert false (* TODO? *) + and aux_children mathonly spacing xref pos prec uris terms = + let find_clusters = + let rec aux_list first clusters acc = + function + [] when acc = [] -> List.rev clusters + | [] -> aux_list first (List.rev acc :: clusters) [] [] + | (A.Layout A.Break) :: tl when acc = [] -> aux_list first clusters [] tl + | (A.Layout A.Break) :: tl -> aux_list first (List.rev acc :: clusters) [] tl + | [hd] -> + let pos' = + if first then + pos + else + match pos with + `None -> `Right + | `Inner -> `Inner + | `Right -> `Right + | `Left -> `Inner + in + aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) [] + | hd :: tl -> + let pos' = + match pos, first with + `None, true -> `Left + | `None, false -> `Inner + | `Left, true -> `Left + | `Left, false -> `Inner + | `Right, _ -> `Inner + | `Inner, _ -> `Inner + in + aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) tl + in + aux_list true [] [] + in + let boxify_pres = function - [] -> [] - | [t] -> - assert (not first); - let pos' = - match pos with - `None -> `Right - | `Inner -> `Inner - | `Right -> `Right - | `Left -> `Inner - in - [aux mathonly xref pos' prec t] - | t :: tl -> - let pos' = - match pos, first with - `None, true -> `Left - | `None, false -> `Inner - | `Left, true -> `Left - | `Left, false -> `Inner - | `Right, _ -> `Inner - | `Inner, _ -> `Inner - in - (aux mathonly xref pos' prec t) :: aux_list false tl + [t] -> t + | tl -> box_of mathonly (A.H, spacing, false) [] tl in - match terms with - [t] -> [aux mathonly xref pos prec t] - | tl -> aux_list true tl + List.map boxify_pres (find_clusters terms) in - aux false None `None 0 + aux [] false None `None 0 [] let render_to_boxml id_to_uri t = let rec print_box (t: CicNotationPres.boxml_markup) =