1 (* Copyright (C
) 2009 Matthew Fluet
.
2 * Copyright (C
) 1999-2006 Henry Cejtin
, Matthew Fluet
, Suresh
3 * Jagannathan
, and Stephen Weeks
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
9 structure Reader
: READER
=
12 type ('a
, 's
) t
= 's
-> ('a
* 's
) option
14 fun char(r
, c
: char
) s
=
17 |
SOME(c
', s
) => if c
= c
' then SOME((), s
) else NONE
34 NONE
=> SOME(rev ac
, s
)
35 |
SOME(x
, s
) => loop(s
, x
:: ac
)
39 fun firstN(r
, n
: Int.t
) s
=
46 |
SOME(x
, s
) => loop(n
- 1, s
, x
:: ac
))
53 |
SOME(a
, s
) => Option
.map(f a
, fn b
=> (b
, s
))
55 fun map(r
, f
) = mapFail(r
, SOME
o f
)
63 |
SOME(x2
, s
) => SOME((x1
, x2
), s
)
66 fun seq3(r1
, r2
, r3
) s
=
75 |
SOME(x3
, s
) => SOME((x1
, x2
, x3
), s
)
77 fun seq4(r1
, r2
, r3
, r4
) s
=
78 case seq3(r1
, r2
, r3
) s
of
80 |
SOME((x1
, x2
, x3
), s
) =>
83 |
SOME(x4
, s
) => SOME((x1
, x2
, x3
, x4
), s
)
85 fun seq5(r1
, r2
, r3
, r4
, r5
) s
=
86 case seq4(r1
, r2
, r3
, r4
) s
of
88 |
SOME((x1
, x2
, x3
, x4
), s
) =>
91 |
SOME(x5
, s
) => SOME((x1
, x2
, x3
, x4
, x5
), s
)
93 fun stringOfLength(r
, i
: Int.t
) s
=
97 then SOME(implode(rev cs
), s
)
100 |
SOME(c
, s
) => loop(i
- 1, s
, c
:: cs
))
104 val info
= Trace
.info
"Reader.readFromString"
106 fun readFromString(rm
, s
) =
107 let val n
: Int.t
= String.size s
108 fun reader(i
: Int.t
) =
110 then SOME(String.sub(s
, i
), i
+ 1)
116 fn NONE
=> Layout
.str
"NONE"
117 |
SOME(c
, _
) => Char.layout c
,
118 fn _
=> (true, fn _
=> true))
120 in case rm
reader (0: Int.t
) of
122 |
SOME(a
, i
) => if i
= n
then SOME a
else NONE