as is
without express or implied warranty. Export of this
software outside of the United States of America may require an
export license.
INTERFACEThink ``strtok''. ATextReader ; IMPORT TextList, Rd; IMPORT Thread; IMPORT FloatMode, Lex;
TextReader.T
is initialized with
txtRd := NEW(TextReader.T).init(string);Tokens may be parsed out by passing in a delimiter, as follows:
VAR txt : TEXT; BEGIN WHILE txtRd.next(" ,.", txt) DO ( parse txt ) END ENDTo get the rest of the line, pass ``'' as the delims. It is a checked run-time error to pass NIL as the delims or as line.
EXCEPTION NoMore; TYPE T <: Public;All the methods of a
TextReader.T
leave the reader in a state
to parse further untouched tokens.
Public = OBJECT METHODS next(delims : TEXT; VAR chunk : TEXT; skipNulls := FALSE) : BOOLEAN;
get next word beforedelims
from reader. IfskipNulls
isTRUE
, zero-length strings are never returned. Return value isTRUE
if call succeeded. If nothing was left, call fails, and returnsFALSE
.
nextS(READONLY delims : SET OF CHAR; VAR chunk : TEXT; skipNulls := FALSE) : BOOLEAN;
next
actually callsnextS
.
nextE(delims : TEXT; skipNulls := FALSE) : TEXT RAISES { NoMore };
same as next
, except failure is signalled thru an exception
nextSE(READONLY delims : SET OF CHAR; skipNulls := FALSE) : TEXT RAISES { NoMore };
same as nextS
, except failure is signalled thru an exception
get() : TEXT RAISES { NoMore };
same as nextE( \t\n\r
, skipNulls := TRUE)
getLR() : LONGREAL RAISES { NoMore, Lex.Error, FloatMode.Trap }; getLongReal() : LONGREAL RAISES { NoMore, Lex.Error, FloatMode.Trap }; getInt() : INTEGER RAISES { NoMore, Lex.Error, FloatMode.Trap }; getCard() : CARDINAL RAISES { NoMore, Lex.Error, FloatMode.Trap }; getBool() : BOOLEAN RAISES { NoMore, Lex.Error }; init(line : TEXT) : T;
initialize a new TextReader.T
initFromRd(rd : Rd.T) : T RAISES { Rd.Failure, Thread.Alerted };
initialize from anRd.T
.rd
must eventually end (to allow in-memory implementations)
isEmpty() : BOOLEAN;
probe a TextReader.T
empty() : BOOLEAN;
alias for above
shatter(listDelims : TEXT; endDelims : TEXT; skipNulls := FALSE) : TextList.T;
tokenize a line intoTEXT
tokens until EOT or an endDelim. It is a checked runtime error for there to be an overlap betweenlistDelims
andendDelims
pushBack(t: TEXT);
insertt
before remaining unreadTEXT
.t
must end in delimiter(s) if the next call tonext
is not to run past the end oft
. Current implementation may or may not insert an extra invisible delimiter aftert
in some cases. This will not be seen ifskipNulls
is alwaysTRUE
andt
always ends in a delimiter anyway.
save() : Continuation; continue(from : Continuation);
permits parsing and returning to an old state
END; TYPE Continuation <: ROOT; CONST Brand = "TextReader"; PROCEDURE New(txt : TEXT) : T;
equiv. to NEW(T).init(txt)
END TextReader.