1 (* Copyright (C
) 1999-2005 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
22 |
List sexps
=> paren (align (List.map (sexps
, layout
)))
25 String.translate (s
, fn c
=>
29 | _
=> String.fromChar c
),
33 val toString
= Layout
.toString
o layout
35 datatype parseResult
=
40 fun parse (peekChar
: unit
-> char option
,
41 getChar
: unit
-> char option
): parseResult
=
43 exception Err
of string
44 fun error s
= raise (Err s
)
45 fun atom (cs
: char list
): t
=
47 fun done () = Atom (String.fromListRev cs
)
53 orelse c
= #
"(" orelse c
= #
")" orelse c
= #
"\""
59 | SOME c
=> atom (c
:: cs
)
61 fun string (cs
: char list
): t
=
63 NONE
=> error
"eof in middle of string"
66 #
"\"" => String (String.fromListRev cs
)
67 | #
"\\" => (case getChar () of
68 NONE
=> error
"eof in middle of string"
69 | SOME c
=> string (c
:: cs
))
70 | _
=> string (c
:: cs
))
71 fun ignoreLine (): bool =
74 | SOME c
=> c
= #
"\n" orelse ignoreLine ()
75 fun sexp (): t option
=
78 | SOME c
=> sexpChar c
79 and sexpChar (c
: char
): t option
=
81 #
"(" => SOME (List (finishList
[]))
82 | #
")" => error
"unmatched )"
83 | #
"\"" => SOME (string [])
84 | #
";" => if ignoreLine ()
87 | _
=> if Char.isSpace c
90 and finishList (elts
: t list
): t list
=
92 NONE
=> error
"unmatched ("
99 else error
"unmatched ("
105 NONE
=> error
"unmatched ("
106 | SOME s
=> finishList (s
:: elts
))
110 | SOME s
=> Sexp s
) handle Err s
=> Error s
114 parse (fn () => In
.peekChar ins
,
115 fn () => In
.inputChar ins
)
119 val n
= String.size s
127 else SOME (String.sub (s
, i
))
131 val res
= peekChar ()
132 val _
= if isSome res
then r
:= 1 + !r
else ()
137 parse (peekChar
, getChar
)