File: ifdef

package info (click to toggle)
pxp 1.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,772 kB
  • ctags: 2,534
  • sloc: ml: 28,666; xml: 2,597; makefile: 821; sh: 691
file content (126 lines) | stat: -rwxr-xr-x 3,508 bytes parent folder | download | duplicates (13)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /bin/sh
# (*
exec ocaml "$0" "$@"
*) directory ".";;

let rec neg_index_from s start c =
  let l = String.length s in
  if start = l then
    raise Not_found
  else if start > l then
    invalid_arg "neg_index_from" 
  else if s.[ start ] = c then
    neg_index_from s (start+1) c
  else
    start
;;


type directive =
    Ifdef of string
  | Ifndef of string
  | Endif
  | No_directive

let get_directive s =
  let l_s = String.length s in
  let is literal =
    let l_literal = String.length literal in
    (l_s >= l_literal) &&
    (String.sub s 0 l_literal = literal) &&
    (if l_s > l_literal then s.[l_literal] = ' ' else true)
  in
  let get_arg() =
    try
      let k1 = String.index s ' ' in
      let k2 = neg_index_from s k1 ' ' in
      let k3 = try String.index_from s k2 ' ' with Not_found -> l_s in
      String.sub s k2 (k3-k2)
    with
	Not_found ->
	  failwith "Argument expected"
  in
  match s with
      _ when is "IFDEF"  -> Ifdef (get_arg())
    | _ when is "IFNDEF" -> Ifndef (get_arg()) 
    | _ when is "ENDIF"  -> Endif
    | _ -> No_directive
;;

let rec filter infile inch outch vars linenr skip =
  (* Copy lines from [inch] to [outch]. The next line read from [inch] will
   * have line number [linenr]. [vars] contains the list of defined variables.
   * [skip] indicates that output is to be suppressed.
   * The function stops at the next ENDIF directive for which no matching
   * IFDEF or IFNDEF has been found before, or at the end of [inch].
   * The function returns the line number of the ENDIF directive.
   *)
  output_string outch ("# " ^ string_of_int linenr ^ " \"" ^ infile ^ "\"\n");
  try
    let current_linenr = ref linenr in
    let current_line = ref (input_line inch) in
    let dir = ref (get_directive !current_line) in
    while !dir <> Endif do
      begin match !dir with
	| Ifdef v ->
	    let skip' = not (List.mem v vars) in
	    let linenr' = 
	      filter infile inch outch vars (!current_linenr+1) (skip || skip') in
	    current_linenr := linenr';
	    output_string 
	      outch 
	      ("# " ^ string_of_int (!current_linenr+1) ^ " \"" ^ infile ^ "\"\n");
	| Ifndef v ->
	    let skip' = List.mem v vars in
	    let linenr' = 
	      filter infile inch outch vars (!current_linenr+1) (skip || skip') in
	    current_linenr := linenr';
	    output_string 
	      outch 
	      ("# " ^ string_of_int (!current_linenr+1) ^ " \"" ^ infile ^ "\"\n");
	| No_directive ->
	    if not skip then 
	      output_string outch (!current_line ^ "\n");
	| Endif -> assert false
      end;

      (* Continue with next line: *)
      incr current_linenr;
      current_line := input_line inch;
      dir := get_directive !current_line;
    done;
    !current_linenr
  with
      End_of_file -> 0
;;

      
let main () =
  let vars = ref [] in
  let infile = ref "" in
  Arg.parse
      [ "-D", Arg.String (fun s -> vars := s :: !vars),
	   "var    define this variable";
      ]
      (fun s -> infile := s)
      "usage: ifdef [ options ] filename.mlp";
  if !infile = "" then
    failwith "No input file!";
  let basename = Filename.chop_extension !infile in
  let outfile = basename ^ ".ml" in
  if !infile = outfile then
    failwith "Input filename ends with .ml, please avoid that";
  let inch = open_in !infile in
  let outch = open_out outfile in
  try
    let _ = filter !infile inch outch !vars 1 false in
    close_out outch;
    close_in inch;
  with
      error ->
	close_out outch;
	close_in inch;
	Sys.remove (outfile)
;;

main();;