]> matita.cs.unibo.it Git - pkg-cerco/acc.git/blob - cparser/Parse.ml
Package description and copyright added.
[pkg-cerco/acc.git] / cparser / Parse.ml
1 (* *********************************************************************)
2 (*                                                                     *)
3 (*              The Compcert verified compiler                         *)
4 (*                                                                     *)
5 (*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
6 (*                                                                     *)
7 (*  Copyright Institut National de Recherche en Informatique et en     *)
8 (*  Automatique.  All rights reserved.  This file is distributed       *)
9 (*  under the terms of the GNU General Public License as published by  *)
10 (*  the Free Software Foundation, either version 2 of the License, or  *)
11 (*  (at your option) any later version.  This file is also distributed *)
12 (*  under the terms of the INRIA Non-Commercial License Agreement.     *)
13 (*                                                                     *)
14 (* *********************************************************************)
15
16 (* Entry point for the library: parse, elaborate, and transform *)
17
18 module CharSet = Set.Make(struct type t = char let compare = compare end)
19
20 let transform_program t p =
21   let run_pass pass flag p = if CharSet.mem flag t then pass p else p in
22   Rename.program
23   (run_pass (AddCasts.program ~all:(CharSet.mem 'C' t)) 'c'
24   (run_pass StructAssign.program 'S'
25   (run_pass StructByValue.program 's'
26   (run_pass Bitfields.program 'f'
27   (run_pass (SimplExpr.program ~volatile:(CharSet.mem 'v' t)) 'e'
28   (run_pass Unblock.program 'b'
29   p))))))
30
31 let parse_transformations s =
32   let t = ref CharSet.empty in
33   let set s = String.iter (fun c -> t := CharSet.add c !t) s in
34   String.iter
35     (function 'b' -> set "b"
36             | 'e' -> set "e"
37             | 'c' -> set "ec"
38             | 'C' -> set "ecC"
39             | 's' -> set "s"
40             | 'S' -> set "esS"
41             | 'v' -> set "ev"
42             | 'f' -> set "bef"
43             |  _  -> ())
44     s;
45   !t
46
47 let preprocessed_file transfs name lb =
48   Errors.reset();
49   let t = parse_transformations transfs in
50   let p =
51     try
52       Rename.program (transform_program t (Elab.elab_preprocessed_file lb))
53     with Parsing.Parse_error ->
54            Errors.error "Error during parsing"; []
55        | Errors.Abort ->
56            [] in
57   if Errors.check_errors() then None else Some p