-
Notifications
You must be signed in to change notification settings - Fork 2
/
Substloc.ml
47 lines (43 loc) · 1.06 KB
/
Substloc.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
open Substloc_lex
let lex = Substloc_lex.loc
let line = Substloc_lex.line
let print_substring = output stdout
let grab oc fn (bline, bchr) (eline, echr) =
let ic = open_in fn in
let lb = Lexing.from_channel ic in
let rec start lnum =
match line lb with
| None -> close ()
| Some ln ->
if lnum = bline then
go ln lnum bchr
else
start (lnum + 1)
and go ln lnum cnum =
if lnum = eline then begin
output oc ln cnum (echr - cnum);
close ()
end else begin
output oc ln cnum (String.length ln - cnum);
match line lb with
| None -> close ()
| Some ln -> go ln (lnum + 1) 0
end
and close () = close_in ic
in start 1
let main ic oc =
let lb = Lexing.from_channel ic in
let rec go () =
match lex lb with
| LOC(fn, start, stop) ->
begin try
grab oc fn start stop
with _ ->
output_string oc (Lexing.lexeme lb)
end; go ()
| CHR c ->
output_char oc c; go ()
| EOI ->
()
in go ()
let () = main stdin stdout