-
Notifications
You must be signed in to change notification settings - Fork 2
/
decomment.ml
31 lines (29 loc) · 1009 Bytes
/
decomment.ml
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
module B = Buffer
let decomment ib =
let l = B.length ib in
let ob = B.create l in
let cat = B.add_char ob in
let rec next k i = let s = i+1 in if s < l then k s else ob
and accept i = match B.nth ib i with
| '/' -> next comment i
| c -> cat c; next accept i
and comment i = match B.nth ib i with
| '/' -> cat ' '; next line i
| '*' -> cat ' '; next block i
| c -> cat '/'; cat c; next accept i
and line i = match B.nth ib i with
| '\n' -> cat '\n'; next accept i
| _ -> next line i
and block i = match B.nth ib i with
| '\n' -> cat '\n'; next block i
| '*' -> next endblock i
| _ -> next block i
and endblock i = match B.nth ib i with
| '/' -> next accept i
| _ -> next block (i-1) (* "... condemned to repeat it." *)
in next accept (-1)
let rec spool b =
if (try (B.add_string b (input_line stdin); true) with End_of_file -> false)
then (B.add_char b '\n'; spool b) else b
;;
B.output_buffer stdout (decomment (spool (B.create 80)))