MispRope.Mesa
Spreitzer, July 30, 1985 3:21:38 pm PDT
DIRECTORY Misp, Rope, TextFind, TextReplace;
MispRope:
CEDAR
PROGRAM
IMPORTS Misp, Rope, TextFind, TextReplace =
BEGIN
LORA: TYPE = LIST OF REF ANY;
ROPE: TYPE = Rope.ROPE;
Matching: TYPE = REF MatchingRep;
MatchingRep:
TYPE =
RECORD [
finder: TextFind.Finder,
lastMatchedRope: ROPE];
EvalCat:
PROC [args:
LORA, environment: Misp.Environment, data:
REF
ANY ←
NIL, stack: Misp.Stack]
RETURNS [cooked:
REF
ANY]
--Misp.EvalProc-- = {
ans: ROPE ← NIL;
FOR args ← args, args.rest
WHILE args #
NIL
DO
ans ← ans.Concat[NARROW[args.first]];
ENDLOOP;
cooked ← ans;
};
EvalConsFinder:
PROC [args:
LORA, environment: Misp.Environment, data:
REF
ANY ←
NIL, stack: Misp.Stack]
RETURNS [cooked:
REF
ANY]
--Misp.EvalProc-- = {
literal: BOOL ← TRUE;
word: BOOL ← FALSE;
ignoreCase: BOOL ← FALSE;
addBounds: BOOL ← FALSE;
finder: TextFind.Finder ← NIL;
FOR args ← args, args.rest
WHILE args #
NIL
DO
IF finder # NIL THEN ERROR;
WITH args.first
SELECT
FROM
a:
ATOM =>
SELECT a
FROM
$literal => literal ← TRUE;
$pattern => literal ← FALSE;
$ignoreCase => ignoreCase ← TRUE;
$testCase => ignoreCase ← FALSE;
$word => word ← TRUE;
$addBounds => addBounds ← TRUE;
$anywhere => word ← addBounds ← FALSE;
ENDCASE => ERROR;
r: ROPE => finder ← TextFind.CreateFromRope[pattern: r, literal: literal, word: word, ignoreCase: ignoreCase, addBounds: addBounds];
ENDCASE => ERROR;
ENDLOOP;
IF finder = NIL THEN ERROR;
cooked ← finder;
};
EvalRopeMatch:
PROC [args:
LORA, environment: Misp.Environment, data:
REF
ANY ←
NIL, stack: Misp.Stack]
RETURNS [cooked:
REF
ANY]
--Misp.EvalProc-- = {
subject: ROPE ← NARROW[args.rest.first];
finder: TextFind.Finder;
found: BOOL;
WITH args.first
SELECT
FROM
f: TextFind.Finder => finder ← f;
pattern: ROPE => finder ← TextFind.CreateFromRope[pattern: pattern, addBounds: TRUE];
ENDCASE => ERROR;
found ← TextFind.SearchRope[finder, subject].found;
cooked ← IF found THEN NEW [MatchingRep ← [finder, subject]] ELSE NIL;
};
EvalRopeFind:
PROC [args:
LORA, environment: Misp.Environment, data:
REF
ANY ←
NIL, stack: Misp.Stack]
RETURNS [cooked:
REF
ANY]
--Misp.EvalProc-- = {
subject: ROPE ← NARROW[args.rest.first];
finder: TextFind.Finder;
found: BOOL;
WITH args.first
SELECT
FROM
f: TextFind.Finder => finder ← f;
pattern: ROPE => finder ← TextFind.CreateFromRope[pattern: pattern, addBounds: FALSE];
ENDCASE => ERROR;
found ← TextFind.SearchRope[finder, subject].found;
cooked ← IF found THEN NEW [MatchingRep ← [finder, subject]] ELSE NIL;
};
EvalRopeSubstitute:
PROC [args:
LORA, environment: Misp.Environment, data:
REF
ANY ←
NIL, stack: Misp.Stack]
RETURNS [cooked:
REF
ANY]
--Misp.EvalProc-- = {
m: Matching ← NARROW[args.first];
pattern: ROPE ← NARROW[args.rest.first];
cooked ← TextReplace.MapNamedSubfieldToMatch[m.finder, m.lastMatchedRope].Nest[].Apply[pattern];
};
DefineRopeStuff:
PROC [env: Misp.Environment] = {
Misp.Defun[env, $cat, EvalCat];
Misp.Defun[env, $consFinder, EvalConsFinder];
Misp.Defun[env, $consFinderq, EvalConsFinder, FALSE];
Misp.Defun[env, $ropeMatch, EvalRopeMatch];
Misp.Defun[env, $ropeFind, EvalRopeFind];
Misp.Defun[env, $ropeSubstitute, EvalRopeSubstitute];
};
Start:
PROC = {
Misp.RegisterPrimitiveDefiner[DefineRopeStuff];
};
Start[];
END.