]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/mQueryUtil.ml
Initial revision
[helm.git] / helm / ocaml / mathql / mQueryUtil.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                     Ferruccio Guidi <fguidi@cs.unibo.it>                   *)
31 (*                                 30/04/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36
37 (* text linearization and parsing *******************************************)
38
39 let rec txt_list f s = function
40    | []        -> ""
41    | [a]       -> f a
42    | a :: tail -> f a ^ s ^ txt_list f s tail
43    
44 let txt_str s = "\"" ^ s ^ "\""
45
46 let txt_path (p0, p1) =
47    txt_str p0 ^ (if p1 <> [] then "/" ^ txt_list txt_str "/" p1 else "")
48
49 let txt_svar sv = "%" ^ sv 
50
51 let txt_rvar rv = "@" ^ rv 
52
53 let txt_vvar vv = "$" ^ vv 
54
55 let text_of_query x =
56    let module M = MathQL in
57    let txt_inv i = if i then "inverse " else "" in
58    let txt_ref = function
59       | M.RefineExact -> ""
60       | M.RefineSub   -> "sub "
61       | M.RefineSuper -> "super "
62    in
63    let txt_refpath i r p = txt_inv i ^ txt_ref r ^ txt_path p ^ " " in
64    let txt_assign (pl, pr) = txt_vvar (fst pl) ^ " <- " ^ txt_path pr in
65    let rec txt_val = function
66       | M.Const [s]             -> txt_str s
67       | M.Const l               -> "{" ^ txt_list txt_str ", " l ^ "}"
68       | M.VVar vv               -> txt_vvar vv
69       | M.Record (rv, p)        -> txt_rvar rv ^ "." ^ txt_vvar (fst p)
70       | M.Fun (s, x)            -> "fun " ^ txt_str s ^ " " ^ txt_val x
71       | M.Property (i, r, p, x) -> "property " ^ txt_refpath i r p ^ txt_val x
72       | M.RefOf x               -> "refof " ^ txt_set x
73    and txt_boole = function
74       | M.False       -> "false"
75       | M.True        -> "true"
76       | M.Ex b x      -> "ex " ^ txt_boole x
77 (*    | M.Ex b x      -> "ex [" ^ txt_list txt_rvar "," b ^ "] " ^ txt_boole x
78 *)    | M.Not x       -> "not " ^ txt_boole x
79       | M.And (x, y)  -> "(" ^ txt_boole x ^ " and " ^ txt_boole y ^ ")"
80       | M.Or (x, y)   -> "(" ^ txt_boole x ^ " or " ^ txt_boole y ^ ")"
81       | M.Sub (x, y)  -> "(" ^ txt_val x ^ " sub " ^ txt_val y ^ ")"
82       | M.Meet (x, y) -> "(" ^ txt_val x ^ " meet " ^ txt_val y ^ ")"
83       | M.Eq (x, y)   -> "(" ^ txt_val x ^ " eq " ^ txt_val y ^ ")"
84    and txt_set = function
85       | M.SVar sv                   -> txt_svar sv
86       | M.RVar rv                   -> txt_rvar rv
87       | M.Relation (i, r, p, M.Ref x, []) -> "relation " ^ txt_refpath i r p ^ txt_val x
88       | M.Relation (i, r, p, M.Ref x, l)  -> "relation " ^ txt_refpath i r p ^ txt_val x ^ " attr " ^ txt_list txt_assign ", " l
89       | M.Union (x, y)              -> "(" ^ txt_set x ^ " union " ^ txt_set y ^ ")"
90       | M.Intersect (x, y)          -> "(" ^ txt_set x ^ " intersect " ^ txt_set y ^ ")"
91       | M.Diff (x, y)               -> "(" ^ txt_set x ^ " diff " ^ txt_set y ^ ")"
92       | M.LetSVar (sv, x, y)        -> "let " ^ txt_svar sv ^ " be " ^ txt_set x ^ " in " ^ txt_set y
93       | M.LetVVar (vv, x, y)        -> "let " ^ txt_vvar vv ^ " be " ^ txt_val x ^ " in " ^ txt_set y
94       | M.Select (rv, x, y)         -> "select " ^ txt_rvar rv ^ " in " ^ txt_set x ^ " where " ^ txt_boole y
95       | M.Pattern x                 -> "pattern " ^ txt_val x
96       | M.Ref x                     -> "ref " ^ txt_val x
97       | _                           -> assert false
98    in 
99    txt_set x
100
101 let text_of_result x sep =
102    let txt_attr = function
103       | (p, []) -> txt_vvar (fst p)
104       | (p, l)  -> txt_vvar (fst p) ^ " = " ^ txt_list txt_str ", " l
105    in
106    let txt_group l = "{" ^ txt_list txt_attr "; " l ^ "}" in
107    let txt_res = function
108       | (s, []) -> txt_str s 
109       | (s, l)  -> txt_str s ^ " attr " ^ txt_list txt_group ", " l
110    in   
111    let txt_set l = txt_list txt_res ("; " ^ sep) l ^ sep in
112    txt_set x
113
114 let query_of_text lexbuf =
115    MQueryTParser.query MQueryTLexer.query_token lexbuf 
116
117 let result_of_text lexbuf =
118    MQueryTParser.result MQueryTLexer.result_token lexbuf 
119
120 (* conversion functions *****************************************************)
121
122 type uriref = UriManager.uri * (int list)
123
124 let string_of_uriref (uri, fi) =
125    let module UM = UriManager in
126    let str = UM.string_of_uri uri in
127    let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
128    match fi with
129       | []          -> str 
130       | [t]         -> str ^ xp t ^ ")" 
131       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"