File: parse.ml

package info (click to toggle)
camlidl 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,592 kB
  • sloc: ml: 5,238; ansic: 945; cpp: 908; makefile: 358; xml: 213; sh: 74
file content (69 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(***********************************************************************)
(*                                                                     *)
(*                              CamlIDL                                *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 1999 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the GNU Lesser General Public License LGPL v2.1 *)
(*                                                                     *)
(***********************************************************************)

(* $Id: parse.ml,v 1.7 2002-04-19 14:42:20 xleroy Exp $ *)

(* Source parsing *)

open Printf
open Utils
open Linenum

let read_source_file sourcename filename =
  let ic = open_in_bin filename in
  let lb = Lexing.from_channel ic in
  let saved_current_file = !Linenum.current_file
  and saved_current_lexbuf = !Linenum.current_lexbuf in
  Linenum.current_file := filename;
  Linenum.current_lexbuf := lb;
  try
    let res = Parser_midl.file Lexer_midl.token lb in
    close_in ic;
    Linenum.current_file := saved_current_file;
    Linenum.current_lexbuf := saved_current_lexbuf;
    res
  with Parsing.Parse_error ->
         close_in ic;
         eprintf "%t: syntax error\n" print_location;
         raise Error
     | Lexer_midl.Lex_error msg ->
         close_in ic;
         eprintf "%t: %s\n" print_location msg;
         raise Error

let read_file filename =
  if not !Clflags.use_cpp then
    read_source_file filename filename
  else begin
    let tempfile = Filename.temp_file "camlidl" ".idl" in
    try
      if Sys.command
           (sprintf "%s %s %s %s > %s"
                    !Clflags.preprocessor
                    (String.concat " "
                        (List.map (fun s -> "-I" ^ s) !Clflags.search_path))
                    (String.concat " "
                        (List.map (fun s -> "-D" ^ s) !Clflags.prepro_defines))
                    filename
                    tempfile)
         <> 0
       then error "error during preprocessing";
       let r = read_source_file filename tempfile in
       remove_file tempfile;
       r
     with x ->
       remove_file tempfile;
       raise x
  end

let _ =
  Parse_aux.read_file := read_file