-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ml
More file actions
33 lines (25 loc) · 1015 Bytes
/
example.ml
File metadata and controls
33 lines (25 loc) · 1015 Bytes
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
open Parsing
let process lexbuf =
let lexer = SexpLexer.lexer lexbuf in
try
match parse lexer lexbuf with
| OK ast ->
(match ast with Sexp.EndFile -> raise EndFile | _ -> ());
Format.fprintf Format.std_formatter "%a\n%!" Sexp.pp_sexp ast
| ERR (pos, msg) ->
Format.fprintf Format.std_formatter "*** %a %s%!" Sexp.pp_fpos pos msg
with
(* abandon the current phrase on a lexer error *)
SexpLexer.LexError (pos, msg) ->
Format.fprintf Format.std_formatter "*** Lexing error: %s at %a\n%!" msg Sexp.pp_pos pos
let _ =
let chan = open_in "/dev/stdin" in
let istty = Unix.isatty @@ Unix.descr_of_in_channel chan in
let lexbuf = Sedlexing.Utf8.from_channel ~chunk_size:(if istty then 1 else 1024) chan in
Sedlexing.set_filename lexbuf "/dev/stdin";
if istty then
Sedlexing.set_prompter lexbuf (fun () -> Format.printf "> %!");
try
while true do process lexbuf done
with
EndFile -> ()