Types
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
Version: TYPE = RECORD[major, minor: CARDINAL];
TokenType:
TYPE = {nil,
op, -- value is token.op
number, -- value is token.number
int, rational, real, -- value from GetInt, GetRational, GetReal
identifier, comment, insertFile, -- value from GetRope
string, largeVec,
eof -- end of file
};
Token:
TYPE =
RECORD[
index: Index, -- starting index in the master for this token
next: Index, -- use this index to get the next token
type: TokenType, -- what kind of token
op: PrimitiveOrSymbol, -- primitive or symbol, if type=op (nil otherwise)
number: INTEGER, -- short number value, if type=number
length: INT -- number of data bytes, if applicable
];
nullToken: Token = [index: 0, next: 0, type: nil, op: nil, number: 0, length: 0];
Error: ERROR[message: ROPE];
Reading
Reader: TYPE = REF ReaderRep;
ReaderRep:
TYPE =
RECORD[
procs: REF READONLY ReaderProcs,
ropeReader: RopeReader.Ref,
buffer: REF TEXT,
bytesRemaining: INT,
startingIndex: INT
];
ReaderProcs:
TYPE =
RECORD[
GetToken: PROC[self: Reader, index: INT] RETURNS[Token],
GetInt: PROC[self: Reader, token: Token] RETURNS[INT],
GetRational: PROC[self: Reader, token: Token] RETURNS[Rational],
GetReal: PROC[self: Reader, token: Token] RETURNS[REAL],
GetRope: PROC[self: Reader, token: Token] RETURNS[ROPE]
];
OpenReader:
PROC[fileName:
ROPE]
RETURNS[reader: Reader];
Prepare to read an Interpress master with the given file name.
Parse the header, and return a reader appropriate for the encoding type.
CreateReader:
PROC[encoding:
ROPE, ropeReader: RopeReader.Ref]
RETURNS[Reader];
Create a reader for a specified encoding.
Use ropeReader.GetIndex[] for startingIndex.
GetStartingIndex:
PROC[self: Reader]
RETURNS[
INT]
= INLINE { RETURN[self.startingIndex] };
GetToken:
PROC[self: Reader, index:
INT]
RETURNS[Token]
= INLINE { RETURN[self.procs.GetToken[self, index]] };
GetInt:
PROC[self: Reader, token: Token]
RETURNS[
INT]
= INLINE { RETURN[self.procs.GetInt[self, token]] };
GetRational:
PROC[self: Reader, token: Token]
RETURNS[Rational]
= INLINE { RETURN[self.procs.GetRational[self, token]] };
GetReal:
PROC[self: Reader, token: Token]
RETURNS[
REAL]
= INLINE { RETURN[self.procs.GetReal[self, token]] };
GetRope:
PROC[self: Reader, token: Token]
RETURNS[
ROPE]
= INLINE { RETURN[self.procs.GetRope[self, token]] };
CloseReader: PROC[self: Reader];
Writing
Writer: TYPE = REF WriterRep;
WriterRep: TYPE = RECORD[procs: REF READONLY WriterProcs, stream: STREAM];
WriterProcs:
TYPE =
RECORD[
PutOp: PROC[self: Writer, op: PrimitiveOrSymbol],
PutInt: PROC[self: Writer, i: INT],
PutRational: PROC[self: Writer, r: Rational],
PutReal: PROC[self: Writer, r: REAL],
PutRope: PROC[self: Writer, type: TokenType, text: ROPE]
];
OpenWriter:
PROC[name:
ROPE, encoding:
ROPE]
RETURNS[Writer];
Prepare to write an Interpress master with the given file name.
Write the header and return a writer for the given encoding.
CreateWriter:
PROC[encoding:
ROPE, stream:
STREAM]
RETURNS[Writer];
Open a writer for a specified encoding; start writing wherever the stream is positioned.
PutOp:
PROC[self: Writer, op: PrimitiveOrSymbol]
= INLINE { self.procs.PutOp[self, op] };
PutInt:
PROC[self: Writer, i:
INT]
= INLINE { self.procs.PutInt[self, i] };
PutRational:
PROC[self: Writer, r: Rational]
= INLINE { self.procs.PutRational[self, r] };
PutReal:
PROC[self: Writer, r:
REAL]
= INLINE { self.procs.PutReal[self, r] };
PutRope:
PROC[self: Writer, type: TokenType, text:
ROPE]
= INLINE { self.procs.PutRope[self, type, text] };
CloseWriter: PROC[self: Writer];
Registering encoding types
ReadProc: TYPE = PROC[ropeReader: RopeReader.Ref] RETURNS[Reader];
WriteProc: TYPE = PROC[stream: STREAM] RETURNS[Writer];
Register: PROC[encoding: ROPE, version: Version, read: ReadProc, write: WriteProc];