1 (* Copyright (C) 2006, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
27 GrafiteParser.ast_statement GrafiteParser.localized_option -> bool
31 | GrafiteParser.LSome (GrafiteAst.Executable (loc, _))
32 | GrafiteParser.LSome (GrafiteAst.Comment (loc, _))
33 | GrafiteParser.LNone loc ->
36 let grep_statement ?(status = LexiconEngine.initial_status) ?(callback = ignore)
37 ~fname ~include_paths test
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 *)
45 try Some (parse_fun status)
46 with End_of_file -> None in
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
52 Netconversion.ustring_sub `Enc_utf8 loc_begin (loc_end - loc_begin)
54 callback raw_statement;
55 exaust (raw_statement :: acc) status
56 | Some (status, _stm) -> exaust acc status in
59 let ma_extension_test fname = Filename.check_suffix fname ".ma"
61 let rgrep_statement ?status ?callback ?(fname_test = ma_extension_test)
62 ~dirname ~include_paths test
64 let files = HExtlib.find ~test:fname_test dirname in
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))