]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/mQueryUtil.ml
75eb86d442c02cf5e59afb731c5c6df235c9b7cf
[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_qstr s = "\"" ^ s ^ "\""
45
46 let text_of_query x =
47    let module M = MathQL in
48    let txt_svar sv = "%" ^ sv in
49    let txt_rvar rv = "@" ^ rv in
50    let txt_vvar vv = "$" ^ vv in
51    let txt_ref = function
52       | M.ExactOp -> ""
53       | M.SubOp   -> "sub "
54       | M.SuperOp -> "super "
55    in
56    let txt_vvar_list l =
57       txt_list txt_vvar "," l
58    in
59    let rec txt_val = function
60       | M.Const [s]           -> txt_qstr s
61       | M.Const l             -> "{" ^ txt_list txt_qstr "," l ^ "}"
62       | M.Record (rv, vv)     -> txt_rvar rv ^ "." ^ txt_rvar vv
63       | M.Fun (s, x)          -> "fun " ^ txt_qstr s ^ " " ^ txt_val x
64       | M.Attribute (r, s, x) -> "attribute " ^ txt_ref r ^ txt_qstr s ^ " " ^ txt_val x
65       | M.RefOf x             -> "refof " ^ txt_set x
66    and txt_boole = function
67       | M.False       -> "false"
68       | M.True        -> "true"
69       | M.Ex x        -> "ex " ^ txt_boole x
70       | M.Not x       -> "not " ^ txt_boole x
71       | M.And (x, y)  -> "(" ^ txt_boole x ^ " and " ^ txt_boole y ^ ")"
72       | M.Or (x, y)   -> "(" ^ txt_boole x ^ " or " ^ txt_boole y ^ ")"
73       | M.Sub (x, y)  -> "(" ^ txt_val x ^ " sub " ^ txt_val y ^ ")"
74       | M.Meet (x, y) -> "(" ^ txt_val x ^ " meet " ^ txt_val y ^ ")"
75       | M.Eq (x, y)   -> "(" ^ txt_val x ^ " eq " ^ txt_val y ^ ")"
76    and txt_set = function
77       | M.SVar sv                -> txt_svar sv
78       | M.RVar rv                -> txt_rvar rv
79       | M.Relation (r, s, x, []) -> "relation " ^ txt_ref r ^ txt_qstr s ^ " " ^ txt_set x
80       | M.Relation (r, s, x, l)  -> "relation " ^ txt_ref r ^ txt_qstr s ^ " " ^ txt_set x ^ " attr " ^ txt_vvar_list l
81       | M.Union (x, y)           -> "(" ^ txt_set x ^ " union " ^ txt_set y ^ ")"
82       | M.Intersect (x, y)       -> "(" ^ txt_set x ^ " intersect " ^ txt_set y ^ ")"
83       | M.Diff (x, y)            -> "(" ^ txt_set x ^ " diff " ^ txt_set y ^ ")"
84       | M.Let (sv, x, y)         -> "let " ^ txt_svar sv ^ " be " ^ txt_set x ^ " in " ^ txt_set y
85       | M.Select (rv, x, y)      -> "select " ^ txt_rvar rv ^ " in " ^ txt_set x ^ " where " ^ txt_boole y
86       | M.Pattern x              -> "pattern " ^ txt_val x
87       | M.Ref x                  -> "ref " ^ txt_val x
88    in
89    txt_set x
90
91 let text_of_result x =
92    let txt_attr = function
93       | (s, []) -> txt_qstr s
94       | (s, l)  -> txt_qstr s ^ "=" ^ txt_list txt_qstr "," l
95    in
96    let txt_group l = "{" ^ txt_list txt_attr ";" l ^ "}" in
97    let txt_res = function
98       | (s, []) -> txt_qstr s 
99       | (s, l)  -> txt_qstr s ^ " attr " ^ txt_list txt_group "," l
100    in   
101    let txt_set l = txt_list txt_res ";" l in
102    txt_set x
103
104 let query_of_text lexbuf =
105    MQueryTParser.query MQueryTLexer.query_token lexbuf 
106
107 let result_of_text lexbuf =
108    MQueryTParser.result MQueryTLexer.result_token lexbuf 
109
110 (*
111
112 (* Converting functions *****************************************************)
113
114 let tref_uref u =
115    let s = str_uref u in
116    MQueryTParser.ref MQueryTLexer.rtoken (Lexing.from_string s) 
117
118 (* implementazione manuale di tref_uref da controllare 
119
120 let split s =
121    try 
122       let i = Str.search_forward (Str.regexp_string ":/") s 0 in
123       let p = Str.string_before s i in
124       let q = Str.string_after s (i + 2) in
125          (p, q)
126    with 
127       Not_found -> (s, "")
128
129 let encode = function
130    | Str.Text s  -> MQBC s
131    | Str.Delim s ->  
132       if s = "?"  then MQBQ else
133       if s = "*"  then MQBS else
134       if s = "**" then MQBSS else
135       if s = "/"  then MQBD  else MQBC s
136
137 let tref_uref (u, i) =
138    let s = UriManager.string_of_uri u in
139    match split s with
140       | (p, q) -> 
141          let rx = Str.regexp "\?\|\*\*\|\*\|/" in
142          let l = Str.full_split rx q in
143          (Some p, List.map encode l, i) 
144
145 *)
146
147 *)