IndexPropertiesImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Created by Rick Beach, July 12, 1983 3:14 pm
Rick Beach, February 27, 1985 9:45:58 pm PST
DIRECTORY
IndexProperties,
IndexToolPrivate,
IndexTree,
IO,
Rope,
TextNode,
TiogaOps,
TiogaOpsDefs,
ViewerClasses;
IndexPropertiesImpl: CEDAR PROGRAM
IMPORTS IndexToolPrivate, IndexTree, IO, Rope, TextNode, TiogaOps
EXPORTS IndexProperties, TiogaOpsDefs
= BEGIN OPEN IndexToolPrivate;
ROPE: TYPE = Rope.ROPE;
NodeBody: PUBLIC TYPE = TextNode.Body;
IndexEntryToRope: PUBLIC PROCEDURE [ix: IndexEntry] RETURNS [rope: ROPE] = {
phrases: LIST OF ROPE ← ix.phrases;
WHILE phrases # NIL DO
rope ← Rope.Concat[rope, IO.PutFR["\"%q\" ", IO.rope[phrases.first]]];
phrases ← phrases.rest;
ENDLOOP;
rope ← Rope.Concat[rope, IO.PutFR["%g ", IO.int[ix.bytePosition]]];
IF ix.primary THEN rope ← Rope.Concat[rope, "Primary "];
SELECT ix.type FROM
single => NULL;
beginRange => rope ← Rope.Concat[rope, "BeginRange "];
endRange => rope ← Rope.Concat[rope, "EndRange "];
see => {
phrases ← ix.seePhrases;
rope ← Rope.Concat[rope, "See "];
WHILE phrases # NIL DO
rope ← Rope.Concat[rope, IO.PutFR["\"%q\" ", IO.rope[phrases.first]]];
phrases ← phrases.rest;
ENDLOOP;
};
ENDCASE;
IF ix.sortAsPhrases # NIL THEN {
phrases ← ix.sortAsPhrases;
rope ← Rope.Concat[rope, "SortAs "];
WHILE phrases # NIL DO
rope ← Rope.Concat[rope, IO.PutFR["\"%q\" ", IO.rope[phrases.first]]];
phrases ← phrases.rest;
ENDLOOP;
};
rope ← Rope.Concat[rope, IO.PutFR["VersionStamp \"%q\"\n", IO.rope[ix.versionStamp]]];
};
IndexPropToEntries: PUBLIC PROCEDURE [rope: ROPE]
RETURNS [entries: LIST OF IndexEntry ← NIL] = {
stream: IO.STREAM;
NextEntry: PROCEDURE RETURNS [entries: LIST OF IndexEntry ← NIL] = {
ix: IndexEntry ← IndexParse[stream];
IF ix # NIL THEN entries ← CONS[ix, NextEntry[]];
};
IF rope # NIL AND rope.Length > 0 THEN {
stream ← IO.RIS[rope];
entries ← NextEntry[];
};
};
UnrecognizedKeyword: PUBLIC SIGNAL = CODE;
KeywordExpectedButMissing: PUBLIC SIGNAL = CODE;
BytePositionMissing: PUBLIC SIGNAL = CODE;
IndexParse: PUBLIC PROCEDURE [stream: IO.STREAM] RETURNS [ix: IndexEntry ← NIL] = {
tokens: LIST OF REFIO.GetRefAnyLine[stream ! IO.EndOfStream => GOTO Quit];
Phrases: PROCEDURE RETURNS [phrases: LIST OF ROPE ← NIL] = {
IF tokens = NIL THEN RETURN;
IF tokens.first # NIL THEN {
p: ROPE;
IF NOT ISTYPE[tokens.first, ROPE] THEN RETURN;
p ← NARROW[tokens.first, ROPE];
tokens ← tokens.rest;
phrases ← CONS[p, Phrases[]];
};
};
BytePosition: PROCEDURE RETURNS [bytePosition: INTEGER] = {
IF tokens = NIL OR NOT ISTYPE[tokens.first, REF INT] THEN SIGNAL BytePositionMissing;
bytePosition ← NARROW[tokens.first, REF INT]^;
tokens ← tokens.rest;
};
Keyword: PROCEDURE RETURNS [keyword: ATOM] = {
IF tokens = NIL THEN RETURN [$EndOfLine];
IF NOT ISTYPE[tokens.first, ATOM] THEN SIGNAL KeywordExpectedButMissing;
keyword ← NARROW[tokens.first, ATOM];
tokens ← tokens.rest;
};
IF tokens = NIL OR tokens.first = NIL THEN RETURN;
ix ← NEW[IndexEntryRec];
ix.phrases ← Phrases[];
ix.bytePosition ← BytePosition[];
ix.type ← single;
WHILE tokens # NIL DO
SELECT Keyword[] FROM
$BeginRange => ix.type ← beginRange;
$EndRange => ix.type ← endRange;
$Primary => ix.primary ← TRUE;
$See => ix.seePhrases ← Phrases[];
$SortAs => ix.seePhrases ← Phrases[];
$VersionStamp => ix.versionStamp ← Phrases[].first;
$EndOfLine => EXIT;
ENDCASE => SIGNAL UnrecognizedKeyword;
ENDLOOP;
EXITS
Quit => NULL;
};
ScanIndexProperties: PUBLIC PROCEDURE [viewer: ViewerClasses.Viewer,
indexHandle: IndexHandle] = {
root: TextNode.Ref ← TiogaOps.ViewerDoc[viewer];
node: TextNode.Ref ← TiogaOps.FirstChild[root];
WHILE node # NIL DO
ixRope: ROPENARROW[TiogaOps.GetProp[node, $Index], ROPE];
entries: LIST OF IndexEntry ← IndexPropToEntries[ixRope];
versionStamp: ROPE ← IndexToolPrivate.IndexVersionStamp[TiogaOps.GetRope[node]];
WHILE entries # NIL DO
ix: IndexEntry ← entries.first;
ix.stickyPointer ← TextNode.LocWithin[root, ix.bytePosition];
ix.valid ← versionStamp.Equal[ix.versionStamp];
IndexTree.InsertNewIndexEntry[indexHandle, entries.first];
entries ← entries.rest;
ENDLOOP;
node ← TiogaOps.StepForward[node];
ENDLOOP;
};
END.