Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions src/basis/__text_stream_io.sml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require "__char_array";
require "__char";
require "__substring";
require "__text_prim_io";

require "__list";

structure TextStreamIO: TEXT_STREAM_IO =
struct
Expand All @@ -82,27 +82,17 @@ struct
in

fun inputLine (f: S.instream) =
let
(* the following function returns a triple:
(l,b,g), where l is a list of characters
b is value of proposition "last char is newline"
g is the input stream at the end
*)

fun loop(i,g) = case S.input1 g of
SOME(c, g') =>
if c = Char.chr 10 then ([c],true,g')
else let val (l,b,g'')= loop(i+1,g') in (c::l,b,g'') end
| NONE => ([],false,g)

val (l,lastCharNewline,f') = loop(0,f)
in
(if l<>[] andalso (not lastCharNewline)
then implode (l@[#"\n"])
else implode l,
f')
end

let
fun some (acc, g) = SOME (implode (List.revAppend (acc, [#"\n"])), g)
fun loop (g, acc) =
case S.input1 g of
SOME(c, g') =>
if c = Char.chr 10 then some (acc, g')
else loop (g', c :: acc)
| NONE => case acc of
[] => NONE
| _ => some (acc, g)
in loop (f, []) end

fun outputSubstr(f:S.outstream, ss:Substring.substring) =
S.output(f,Substring.string ss)
Expand Down
11 changes: 4 additions & 7 deletions src/basis/_text_io.sml
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,13 @@ functor TextIO(include sig
(TextIO'.StreamIO.mkInstream
(OSPrimIO.openString s,""))


fun inputLine (f: TextIO'.instream) =
let
val g0 = TextIO'.getInstream f
val (s,gn) = TextStreamIO.inputLine g0
val _ = TextIO'.setInstream(f,gn)
let val g0 = TextIO'.getInstream f
in
s
case TextStreamIO.inputLine g0 of
NONE => NONE
| SOME (line, g1) => (TextIO'.setInstream(f, g1); SOME line)
end


fun outputSubstr(f:TextIO'.outstream, ss:Substring.substring) =
TextIO'.output(f,Substring.string ss)
Expand Down
2 changes: 1 addition & 1 deletion src/basis/text_io.sml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ signature TEXT_IO =
val setOutstream : (outstream * StreamIO.outstream) -> unit


val inputLine : instream -> string
val inputLine : instream -> string option

val outputSubstr : (outstream * Substring.substring) -> unit

Expand Down
4 changes: 2 additions & 2 deletions src/basis/text_stream_io.sml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ signature TEXT_STREAM_IO =

include STREAM_IO

val inputLine : instream -> (string * instream)
val inputLine : instream -> (string * instream) option

val outputSubstr : (outstream * Substring.substring) -> unit

Expand All @@ -68,4 +68,4 @@ signature TEXT_STREAM_IO =
where type elem = Char.char
where type reader = TextPrimIO.reader
where type writer = TextPrimIO.writer
where type pos = TextPrimIO.pos
where type pos = TextPrimIO.pos
2 changes: 1 addition & 1 deletion src/debugger/_ml_debugger.sml
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,7 @@ functor Ml_Debugger(
(do_quit(); raise Exit)
else ()

val line = TextIO.inputLine TextIO.stdIn
val line = getOpt (TextIO.inputLine TextIO.stdIn, "")
in
parse_command line
end
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/_inspector.sml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ functor Inspector (
val _ = print "Inspector> "
val _ = TextIO.flushOut TextIO.stdOut;
in
case rev (explode (TextIO.inputLine(TextIO.stdIn))) of
case rev (explode (getOpt (TextIO.inputLine(TextIO.stdIn), ""))) of
[] => ()
| (_::tagl) =>
let
Expand Down
9 changes: 3 additions & 6 deletions src/interpreter/_save_image.sml
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,9 @@ struct
| parse1 (a::rest,acc) = parse1 (rest,a::acc)

fun loop acc =
let
val line = TextIO.inputLine instream
in
if line = "" then rev acc
else loop (parse1 (explode line,[])::acc)
end
case TextIO.inputLine instream of
NONE => rev acc
| SOME line => loop (parse1 (explode line,[])::acc)

val items = loop []
handle
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/_tty_listener.sml
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ struct
let
val _ = output_fn(do_prompt ("MLWorks", state))
(* val _ = MLWorks.IO.clear_eof MLWorks.IO.std_in *)
val line = TextIO.inputLine TextIO.stdIn
val line = getOpt (TextIO.inputLine TextIO.stdIn, "")
handle IO.Io _ => ""
val new_state =
(case #3 (ShellTypes.with_toplevel_name title
Expand Down
16 changes: 9 additions & 7 deletions src/main/_sectioned_file.sml
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ struct

fun readSectionedFile filename =
let val instream = TextIO.openIn filename
fun get_line() =
let val line = TextIO.inputLine instream
in if line = ""
then raise (InvalidSectionedFile "Premature EOF")
else line end

fun get_line () =
case TextIO.inputLine instream of
SOME line => line
| NONE => raise (InvalidSectionedFile "Premature EOF")

fun read_section () =
let val header = get_line()
Expand All @@ -139,8 +139,10 @@ struct
val (line, _) = Substring.splitr Char.isSpace line
val item =
getOpt(String.fromString(Substring.string line),"")
in item :: (read_items (n - 1)) end
val stamp = getOpt(String.fromString(TextIO.inputLine instream),"")
in item :: (read_items (n - 1)) end

val firstline = getOpt(TextIO.inputLine instream, "")
val stamp = getOpt(String.fromString(firstline), "")
val (section, _) = read_section()
in (TextIO.closeIn instream; (stamp, section))
handle exn => (TextIO.closeIn instream; raise exn)
Expand Down