make_template.ml 1.84 KB
Newer Older
1
let read_line () = 
Adam Chlipala's avatar
Adam Chlipala committed
2
  try
3
    Some (read_line ())
Adam Chlipala's avatar
Adam Chlipala committed
4 5 6
  with End_of_file -> None

let rec initial last_was_empty =
7
  match read_line () with
Adam Chlipala's avatar
Adam Chlipala committed
8 9 10 11 12 13 14
  | None -> ()
  | Some "(* begin thide *)" ->	thide last_was_empty
  | Some "" ->
      if not (last_was_empty) then
	print_newline ();
      initial true
  | Some line ->
15 16 17
      let idx = try Some (String.index line '(') with Not_found -> None in
      match idx with
        | Some idx ->
18 19
            if String.length line > idx+1 && line.[idx+1] = '*'
              && not (String.length line > idx+4 && String.sub line (idx+2) 3 = " EX") then
20 21 22
              if line.[String.length line - 2] = '*' && line.[String.length line - 1] = ')' then
	        initial last_was_empty
	      else
Adam Chlipala's avatar
Adam Chlipala committed
23
	        comment 1 last_was_empty
24 25 26 27 28 29 30
            else begin
              print_endline line;
              initial false
            end
        | None ->
	    print_endline line;
	    initial false
Adam Chlipala's avatar
Adam Chlipala committed
31

Adam Chlipala's avatar
Adam Chlipala committed
32
and comment count last_was_empty =
33
  match read_line () with
Adam Chlipala's avatar
Adam Chlipala committed
34 35
  | None -> ()
  | Some line ->
Adam Chlipala's avatar
Adam Chlipala committed
36 37 38 39 40 41 42
      if String.length line >= 2 && line.[0] = '(' && line.[1] = '*' then
	if String.length line >= 2 && line.[String.length line - 2] = '*'
	    && line.[String.length line - 1] = ')' then
	  comment count last_was_empty
	else
	  comment (count+1) last_was_empty
      else if String.length line >= 2 && line.[String.length line - 2] = '*'
Adam Chlipala's avatar
Adam Chlipala committed
43
	  && line.[String.length line - 1] = ')' then
Adam Chlipala's avatar
Adam Chlipala committed
44 45 46 47 48 49 50 51 52
	if try
	  let idx = String.index line '(' in
	  idx < String.length line - 1 && line.[idx + 1] = '*'
	with Not_found -> false then
	  comment count last_was_empty
	else if count = 1 then
	  initial last_was_empty
	else
	  comment (count-1) last_was_empty
Adam Chlipala's avatar
Adam Chlipala committed
53
      else
Adam Chlipala's avatar
Adam Chlipala committed
54
	comment count last_was_empty
Adam Chlipala's avatar
Adam Chlipala committed
55 56

and thide last_was_empty =
57
  match read_line () with
Adam Chlipala's avatar
Adam Chlipala committed
58 59 60 61 62
  | None -> ()
  | Some "(* end thide *)" -> initial last_was_empty
  | Some _ -> thide last_was_empty

let () = initial false