<> <> 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.