]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/mQueryUtil.ml
debian release 0.0.4-2:
[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 out f s = function
40    | []        -> ()
41    | [a]       -> f a
42    | a :: tail -> f a; out s; txt_list out f s tail
43    
44 let txt_str out s = out ("\"" ^ s ^ "\"")
45
46 let txt_path out (p0, p1) =
47    txt_str out p0; if p1 <> [] then out "/"; txt_list out (txt_str out) "/" p1
48
49 let txt_svar out sv = out ("%" ^ sv) 
50
51 let txt_rvar out rv = out ("@" ^ rv) 
52
53 let txt_vvar out vv = out ("$" ^ vv) 
54
55 let text_of_query out x sep =
56    let module M = MathQL in
57    let txt_inv i = if i then out "inverse " in
58    let txt_ref = function
59       | M.RefineExact -> ()
60       | M.RefineSub   -> out "sub "
61       | M.RefineSuper -> out "super "
62    in
63    let txt_refpath i r p = txt_inv i; txt_ref r; txt_path out p; out " " in
64    let txt_assign (pl, pr) = txt_vvar out (fst pl); out " <- "; txt_path out pr in
65    let rec txt_val = function
66       | M.Const [s]             -> txt_str out s
67       | M.Const l               -> out "{"; txt_list out (txt_str out) ", " l; out "}"
68       | M.VVar vv               -> txt_vvar out vv
69       | M.Record (rv, p)        -> txt_rvar out rv; out "."; txt_vvar out (fst p)
70       | M.Fun (s, x)            -> out "fun "; txt_str out s; out " "; txt_val x
71       | M.Property (i, r, p, x) -> out "property "; txt_refpath i r p; txt_val x
72       | M.RefOf x               -> out "refof "; txt_set x
73    and txt_boole = function
74       | M.False       -> out "false"
75       | M.True        -> out "true"
76       | M.Ex b x      -> out "ex "; txt_boole x
77 (*    | M.Ex b x      -> "ex [" ^ txt_list txt_rvar "," b ^ "] " ^ txt_boole x
78 *)    | M.Not x       -> out "not "; txt_boole x
79       | M.And (x, y)  -> out "("; txt_boole x; out " and "; txt_boole y; out ")"
80       | M.Or (x, y)   -> out "("; txt_boole x; out " or "; txt_boole y; out ")"
81       | M.Sub (x, y)  -> out "("; txt_val x; out " sub "; txt_val y; out ")"
82       | M.Meet (x, y) -> out "("; txt_val x; out " meet "; txt_val y; out ")"
83       | M.Eq (x, y)   -> out "("; txt_val x; out " eq "; txt_val y; out ")"
84    and txt_set = function
85       | M.SVar sv                   -> txt_svar out sv
86       | M.RVar rv                   -> txt_rvar out rv
87       | M.Relation (i, r, p, M.Ref x, []) -> out "relation "; txt_refpath i r p; txt_val x
88       | M.Relation (i, r, p, M.Ref x, l)  -> out "relation "; txt_refpath i r p; txt_val x; out " attr "; txt_list out txt_assign ", " l
89       | M.Union (x, y)              -> out "("; txt_set x; out " union "; txt_set y; out ")"
90       | M.Intersect (x, y)          -> out "("; txt_set x; out " intersect "; txt_set y; out ")"
91       | M.Diff (x, y)               -> out "("; txt_set x; out " diff "; txt_set y; out ")"
92       | M.LetSVar (sv, x, y)        -> out "let "; txt_svar out sv; out " be "; txt_set x; out " in "; txt_set y
93       | M.LetVVar (vv, x, y)        -> out "let "; txt_vvar out vv; out " be "; txt_val x; out " in "; txt_set y
94       | M.Select (rv, x, y)         -> out "select "; txt_rvar out rv; out " in "; txt_set x; out " where "; txt_boole y
95       | M.Pattern x                 -> out "pattern "; txt_val x
96       | M.Ref x                     -> out "ref "; txt_val x
97       | _                           -> assert false
98    in 
99    txt_set x; out sep
100
101 let text_of_result out x sep =
102    let txt_attr = function
103       | (p, []) -> txt_vvar out (fst p)
104       | (p, l)  -> txt_vvar out (fst p); out " = "; txt_list out (txt_str out) ", " l
105    in
106    let txt_group l = out "{"; txt_list out txt_attr "; " l; out "}" in
107    let txt_res = function
108       | (s, []) -> txt_str out s 
109       | (s, l)  -> txt_str out s; out " attr "; txt_list out txt_group ", " l
110    in   
111    let txt_set l = txt_list out txt_res ("; " ^ sep) l in
112    txt_set x; out sep
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 ^ ")"