]> matita.cs.unibo.it Git - helm.git/blob - components/grafite_parser/grafiteWalker.ml
tagged 0.5.0-rc1
[helm.git] / components / grafite_parser / grafiteWalker.ml
1 (* Copyright (C) 2006, 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://helm.cs.unibo.it/
24  *)
25
26 type statement_test =
27   GrafiteParser.ast_statement GrafiteParser.localized_option -> bool
28
29 let get_loc =
30   function
31   | GrafiteParser.LSome (GrafiteAst.Executable (loc, _))
32   | GrafiteParser.LSome (GrafiteAst.Comment (loc, _))
33   | GrafiteParser.LNone loc ->
34       loc
35
36 let grep_statement ?(status = LexiconEngine.initial_status) ?(callback = ignore)
37   ~fname ~include_paths test
38 =
39   let content = HExtlib.input_file fname in
40   let lexbuf = Ulexing.from_utf8_string content in
41   let parse_fun status =
42     GrafiteParser.parse_statement lexbuf ~include_paths status in
43   let rec exaust acc status = (* extract "interesting" statement locations *)
44     let stm =
45       try Some (parse_fun status)
46       with End_of_file -> None in
47     match stm with
48     | None -> List.rev acc
49     | Some (status, stm) when test stm -> (* "interesting" statement *)
50         let loc_begin, loc_end = HExtlib.loc_of_floc (get_loc stm) in
51         let raw_statement =
52           Netconversion.ustring_sub `Enc_utf8 loc_begin (loc_end - loc_begin)
53             content in
54         callback raw_statement;
55         exaust (raw_statement :: acc) status
56     | Some (status, _stm) -> exaust acc status in
57   exaust [] status
58
59 let ma_extension_test fname = Filename.check_suffix fname ".ma"
60
61 let rgrep_statement ?status ?callback ?(fname_test = ma_extension_test)
62   ~dirname ~include_paths test
63 =
64   let files = HExtlib.find ~test:fname_test dirname in
65   List.flatten
66     (List.map
67       (fun fname ->
68         let callback =
69           match callback with
70           | None -> None
71           | Some f -> Some (fun s -> f (fname, s)) in
72         List.map (fun raw -> fname, raw)
73           (grep_statement ?status ?callback ~fname ~include_paths test))
74       files)
75