Debian Codegraph @

regression/textio.3.sml

raw
1 (* See https://github.com/MLton/mlton/issues/535. *)
2
3 datatype tree = Leaf of char | Node of tree * tree
4
5 fun nextis c cs =
6 case TextIO.StreamIO.input1 (TextIO.getInstream cs) of
7 NONE => false
8 | SOME (c', cs') => c' = c
9
10 (* This version works. *)
11 (*
12 fun nextis c cs =
13 case TextIO.lookahead cs of
14 NONE => false
15 | SOME c' => c' = c
16 *)
17
18 fun discard c cs =
19 let val c' = valOf (TextIO.input1 cs) in
20 if c = c'
21 then ()
22 else raise Fail ("unexpected character: " ^ Char.toString c' ^ ", expecting: " ^ Char.toString c)
23 end
24
25 fun parse cs =
26 case valOf (TextIO.input1 cs) of
27 #"(" =>
28 let
29 fun loop l r =
30 if nextis #")" cs
31 then (discard #")" cs; Node (l, r))
32 else loop (Node (l, r)) (parse cs)
33 in
34 loop (parse cs) (parse cs)
35 end
36 | c => Leaf c
37
38 val cs = TextIO.openString "(a(bc)d)"
39
40 (* with this it works too *)
41 (* val true = nextis #"(" cs *)
42
43 val t = (parse cs)