[m-rev.] for review: add string.line type
Peter Wang
wangp at students.csse.unimelb.edu.au
Thu Apr 19 12:25:08 AEST 2007
Estimated hours taken: 1
Branches: main
Introduce a standard way to read lines efficiently but generically using
stream.get/4.
library/string.m:
Add the type string.line.
library/io.m:
Make io.input_stream an instance of stream.reader with unit
string.line.
compiler/error_util.m:
Rename error_util.line to error_line to disambiguate it from
string.line.
extras/net/tcp.m:
Remove the tcp.line type and use string.line instead.
NEWS:
Announce the change.
Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.453
diff -u -r1.453 NEWS
--- NEWS 12 Apr 2007 04:31:09 -0000 1.453
+++ NEWS 19 Apr 2007 02:13:48 -0000
@@ -137,6 +137,10 @@
* We have added a function construct.get_functor_lex/2 which converts
an ordinal functor number into a lexicographic functor number.
+* We have added a type `string.line' and made input streams an instance
+ of stream.reader/4 with that unit type. This means stream.get/4
+ can be used to efficiently read lines.
+
Changes to the Mercury compiler:
* The compiler now issues a warning when an inst declaration is not
Index: compiler/error_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/error_util.m,v
retrieving revision 1.61
diff -u -r1.61 error_util.m
--- compiler/error_util.m 19 Jan 2007 07:04:11 -0000 1.61
+++ compiler/error_util.m 19 Apr 2007 02:13:48 -0000
@@ -841,7 +841,7 @@
indent_increment = 2.
-:- pred write_lines(list(line)::in, maybe(prog_context)::in, int::in,
+:- pred write_lines(list(error_line)::in, maybe(prog_context)::in, int::in,
io::di, io::uo) is det.
write_lines([], _, _, !IO).
@@ -852,7 +852,7 @@
;
MaybeContext = no
),
- Line = line(LineIndent, LineWords),
+ Line = error_line(LineIndent, LineWords),
Indent = FixedIndent + LineIndent * indent_increment,
string.pad_left("", ' ', Indent, IndentStr),
io.write_string(IndentStr, !IO),
@@ -1158,8 +1158,8 @@
%----------------------------------------------------------------------------%
-:- type line
- ---> line(
+:- type error_line
+ ---> error_line(
int, % Indent level; multiply by indent_increment
% to get number of spaces of indentation.
list(string) % The words on the line.
@@ -1171,7 +1171,7 @@
% at least one line.
%
:- pred group_words(bool::in, int::in, list(paragraph)::in, int::in,
- list(line)::out) is det.
+ list(error_line)::out) is det.
group_words(IsFirst, CurIndent, Paras, Max, Lines) :-
(
@@ -1189,7 +1189,7 @@
),
NextIndent = RestIndent + FirstIndentDelta,
- BlankLine = line(CurIndent, []),
+ BlankLine = error_line(CurIndent, []),
list.duplicate(NumBlankLines, BlankLine, BlankLines),
(
FirstParaWords = [],
@@ -1199,7 +1199,7 @@
FirstParaWords = [FirstWord | LaterWords],
get_line_of_words(FirstWord, LaterWords, CurIndent, Max,
LineWords, RestWords),
- CurLine = line(CurIndent, LineWords),
+ CurLine = error_line(CurIndent, LineWords),
group_nonfirst_line_words(RestWords, RestIndent, Max,
ParaRestLines),
@@ -1211,7 +1211,7 @@
).
:- pred group_nonfirst_line_words(list(string)::in, int::in, int::in,
- list(line)::out) is det.
+ list(error_line)::out) is det.
group_nonfirst_line_words(Words, Indent, Max, Lines) :-
(
@@ -1221,7 +1221,7 @@
Words = [FirstWord | LaterWords],
get_line_of_words(FirstWord, LaterWords, Indent, Max,
LineWords, RestWords),
- Line = line(Indent, LineWords),
+ Line = error_line(Indent, LineWords),
group_nonfirst_line_words(RestWords, Indent, Max, RestLines),
Lines = [Line | RestLines]
).
Index: extras/net/tcp.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/net/tcp.m,v
retrieving revision 1.3
diff -u -r1.3 tcp.m
--- extras/net/tcp.m 18 Apr 2007 05:12:37 -0000 1.3
+++ extras/net/tcp.m 19 Apr 2007 02:13:48 -0000
@@ -17,6 +17,7 @@
:- interface.
:- import_module io.
:- import_module stream.
+:- import_module string.
:- type tcp.
:- type bound_tcp.
@@ -52,8 +53,6 @@
:- type error.
-:- type line ---> line(string).
-
:- instance stream(tcp, io.state).
:- instance error(tcp.error).
@@ -86,7 +85,6 @@
:- import_module char.
:- import_module list.
:- import_module require.
-:- import_module string.
:- type tcp
---> tcp(
Index: library/io.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.378
diff -u -r1.378 io.m
--- library/io.m 30 Mar 2007 03:05:00 -0000 1.378
+++ library/io.m 19 Apr 2007 02:13:48 -0000
@@ -1461,6 +1461,7 @@
:- instance stream.stream(io.input_stream, io).
:- instance stream.input(io.input_stream, io, io.error).
:- instance stream.reader(io.input_stream, char, io, io.error).
+:- instance stream.reader(io.input_stream, line, io, io.error).
:- instance stream.line_oriented(io.input_stream, io).
:- instance stream.putback(io.input_stream, char, io, io.error).
@@ -9073,6 +9074,24 @@
)
].
+:- instance stream.reader(io.input_stream, line, io, io.error)
+ where
+[
+ ( get(Stream, Result, !IO) :-
+ io.read_line_as_string(Stream, Result0, !IO),
+ (
+ Result0 = ok(String),
+ Result = ok(line(String))
+ ;
+ Result0 = eof,
+ Result = eof
+ ;
+ Result0 = error(Error),
+ Result = error(Error)
+ )
+ )
+].
+
:- instance stream.putback(io.input_stream, char, io, io.error) where
[
pred(unget/4) is io.putback_char
Index: library/string.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.257
diff -u -r1.257 string.m
--- library/string.m 18 Mar 2007 23:35:00 -0000 1.257
+++ library/string.m 19 Apr 2007 02:13:48 -0000
@@ -40,6 +40,12 @@
%-----------------------------------------------------------------------------%
+ % This type is used for defining stream typeclass instances where the raw
+ % string type would be ambiguous.
+ %
+:- type line
+ ---> line(string).
+
% Determine the length of a string.
% An empty string has length zero.
%
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list