--File: ExprReadImpl.mesa
Last Edited by: Preas, August 2, 1986 7:46:50 pm PDT
DIRECTORY
ExprRead,
FS,
IO,
Rope,
SymTab;
ExprReadImpl: CEDAR PROGRAM
IMPORTS FS, IO, Rope, SymTab
EXPORTS ExprRead = BEGIN OPEN ExprRead;
ReadFile: PUBLIC PROC [fileName: Rope.ROPE] RETURNS [tab: SymTab.Ref ← SymTab.Create[]] ~ {
read the data file and put entries in symbol table
ENABLE {
IO.EndOfStream => GOTO done;
IO.Error => GOTO done;
};
stream: FS.STREAMFS.StreamOpen[fileName, $read];
tokenKind: IO.TokenKind;
key, value, gets, semi: Rope.ROPE;
[tokenKind, key, ] ← IO.GetCedarTokenRope[stream];
WHILE tokenKind # tokenERROR DO --AND tokenKind # tokenEOF DO
[tokenKind, gets, ] ← IO.GetCedarTokenRope[stream];
IF tokenKind # tokenSINGLE AND ~Rope.Equal[gets, "←"] THEN
Error[Rope.Cat["Expecting ←, got: ", gets]];
-- [tokenKind, value, ] ← IO.GetCedarTokenRope[stream];
value ← IO.GetRopeLiteral[stream];
IF tokenKind = tokenERROR OR tokenKind = tokenEOF THEN
Error["Unexpected end of file"];
[] ← SymTab.Store[tab, key, value];
[tokenKind, semi, ] ← IO.GetCedarTokenRope[stream];
IF ~tokenKind # tokenSINGLE AND Rope.Equal[gets, ";"] THEN
Error[Rope.Cat["Expecting ;, got: ", semi]];
[tokenKind, key, ] ← IO.GetCedarTokenRope[stream];
-- IF tokenKind = tokenSINGLE AND Rope.Equal[key, "."] THEN tokenKind ← tokenEOF;
ENDLOOP;
EXITS
done => NULL;
};
FetchRope: PUBLIC PROC [tab: SymTab.Ref, key: Rope.ROPE, missingOK: BOOLEAN] RETURNS [found: BOOLEAN, val: Rope.ROPE] ~ {
get a ROPE entry
ref: REF;
[found, ref] ← SymTab.Fetch[tab, key];
IF ~missingOK AND ~found THEN Error[Rope.Cat["No value for token: ", key, "on fetch"]];
IF found THEN val ← NARROW[ref];
};
FetchBool: PUBLIC PROC [tab: SymTab.Ref, key: Rope.ROPE, missingOK: BOOLEAN] RETURNS [found: BOOLEAN, val: BOOLEAN] ~ {
get a BOOLEAN entry
ref: REF;
[found, ref] ← SymTab.Fetch[tab, key];
IF ~missingOK AND ~found THEN Error[Rope.Cat["No value for token: ", key, "on fetch"]];
IF found THEN {
ropeValue: Rope.ROPE;
ropeValue ← NARROW[ref];
IF Rope.Equal[ropeValue, "TRUE"] THEN val ← TRUE
ELSE IF Rope.Equal[ropeValue, "FALSE"] THEN val ← FALSE
ELSE Error[Rope.Cat["Invalid value for: ", key, "was", ropeValue]];
};
};
Error: PUBLIC ERROR [msg: Rope.ROPENIL] = CODE;
END.