diff options
| author | Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr> | 2002-02-08 10:14:31 +0000 |
|---|---|---|
| committer | Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr> | 2002-02-08 10:14:31 +0000 |
| commit | c1e0dad2a8200e56dc4d15c0314d4cde7a80628c (patch) | |
| tree | faddc95d6739c75b5f8c5d26e3944807200b4b46 /driver/pparse.ml | |
| parent | bd7a6924b6d9630c4b483adf98d519b2d9ecfa20 (diff) | |
| download | ocaml-c1e0dad2a8200e56dc4d15c0314d4cde7a80628c.tar.gz | |
Regroupement du preprocessage et de la lecture directe des fichiers source
à arbre de syntaxe, dans un nouveau fichier pparse.ml commun a compile.ml
et optcompile.ml
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@4365 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'driver/pparse.ml')
| -rw-r--r-- | driver/pparse.ml | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/driver/pparse.ml b/driver/pparse.ml new file mode 100644 index 0000000000..bcec36e7e7 --- /dev/null +++ b/driver/pparse.ml @@ -0,0 +1,79 @@ +(***********************************************************************) +(* *) +(* Objective Caml *) +(* *) +(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *) +(* *) +(* Copyright 2002 Institut National de Recherche en Informatique et *) +(* en Automatique. All rights reserved. This file is distributed *) +(* under the terms of the Q Public License version 1.0. *) +(* *) +(***********************************************************************) + +(* $Id$ *) + +open Format + +exception Error + +(* Optionally preprocess a source file *) + +let preprocess sourcefile = + match !Clflags.preprocessor with + None -> sourcefile + | Some pp -> + let tmpfile = Filename.temp_file "camlpp" "" in + let comm = Printf.sprintf "%s %s > %s" pp sourcefile tmpfile in + if Ccomp.command comm <> 0 then begin + Misc.remove_file tmpfile; + raise Error; + end; + tmpfile + +let remove_preprocessed inputfile = + match !Clflags.preprocessor with + None -> () + | Some _ -> Misc.remove_file inputfile + +let remove_preprocessed_if_ast inputfile = + match !Clflags.preprocessor with + None -> () + | Some _ -> + if inputfile <> !Location.input_name then Misc.remove_file inputfile + +(* Parse a file or get a dumped syntax tree in it *) + +exception Outdated_version + +let file ppf inputfile parse_fun ast_magic = + let ic = open_in_bin inputfile in + let is_ast_file = + try + let buffer = String.create (String.length ast_magic) in + really_input ic buffer 0 (String.length ast_magic); + if buffer = ast_magic then true + else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then + raise Outdated_version + else false + with + Outdated_version -> + Misc.fatal_error "Ocaml and preprocessor have incompatible versions" + | _ -> false + in + let ast = + try + if is_ast_file then begin + if !Clflags.fast then + fprintf ppf "@[Warning: %s@]@." + "option -unsafe used with a preprocessor returning a syntax tree"; + Location.input_name := input_value ic; + input_value ic + end else begin + seek_in ic 0; + Location.input_name := inputfile; + parse_fun (Lexing.from_channel ic) + end + with x -> close_in ic; raise x + in + close_in ic; + ast |
