TreeFind.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
written by Bill Paxton, June 1981
last edit by Bill Paxton, December 30, 1982 10:30 am
Doug Wyatt, March 2, 1985 5:25:56 pm PST
Michael Plass, March 21, 1985 2:55:53 pm PST
Rick Beach, March 27, 1985 1:00:13 pm PST
This module provides a "Find" operation for text in trees
The pattern matching is done by TextFind.Mesa
This module simply walks the tree (forward or backwards) applying the text
DIRECTORY
Rope USING [ROPE],
TextFind USING [Finder],
TextNode USING [Ref, RefTextNode];
TreeFind: CEDAR DEFINITIONS
= BEGIN
Ref: TYPE = TextNode.Ref;
RefTextNode: TYPE = TextNode.RefTextNode;
ROPE: TYPE = Rope.ROPE;
Finder: TYPE = TextFind.Finder;
Create: PROC [pattern: RefTextNode, literal, word, ignoreLooks, ignoreCase, addBounds: BOOLFALSE, patternStart: INT ← 0, patternLen: INTLAST[INT]] RETURNS [Finder];
creates a record containing a "compiled" version of the pattern
see TextFind.Mesa for more info about patterns
if literal is true, each character in pattern is taken literally
if word flag is true, pattern only matches if no adjacent letters or digits
if ignoreLooks if true, then ignore the looks of the pattern characters
if ignoreCase is false, then alpha chars in pattern must match case
otherwise, they can match either upper or lower
if addBounds is true, add |'s to both ends of pattern
CreateFromRope: PROC [pattern: ROPE, literal, word, ignoreCase, addBounds: BOOLFALSE, patternStart: INT ← 0, patternLen: INTLAST[INT]] RETURNS [Finder];
CommentControl: TYPE = { includeComments, excludeComments, commentsOnly };
Try: PROC [finder: Finder,
first: Ref,
start: INT ← 0,
last: Ref ← NIL,
lastLen: INTLAST[INT],
looksExact: BOOLFALSE,
commentControl: CommentControl ← includeComments,
checkFormat: BOOLFALSE,
format: ATOMNIL,
checkStyle: BOOLFALSE,
style: ATOMNIL,
styleProc: PROC [Ref] RETURNS [ATOM] ← NIL,
interrupt: REF BOOLNIL
]
RETURNS [found: BOOL, where: RefTextNode, at, atEnd, before, after: INT];
searches first node beginning at start
continues until finds a match or reaches last node
seaches lastLen chars in last node; seaches to end of other nodes
if finds a match, returns with found = true,
at = beginning of match or location corresponding to { in pattern
atEnd = end of match or location corresponding to } in pattern
after = end of entire match, which may be > atEnd if used } in pattern
before = start of entire match, which may be < at if used { in pattern
if looksExact is true, then match by equality, else by subset
commentControl allows restriction of search to comments/noncomments
if checkFormat is true, then only match in nodes with given format
if checkStyle is true, then only match in nodes with given style
styleProc is supplied so called can determine style for nodes
if just interested in format/style, can pass finder=NIL
if interrupt^ becomes true, will stop searching
TryBackwards: PROC [
finder: Finder,
first: Ref,
len: INTLAST[INT],
last: Ref ← NIL,
lastStart: INT ← 0,
looksExact: BOOLFALSE,
commentControl: CommentControl ← includeComments,
checkFormat: BOOLFALSE,
format: ATOMNIL,
checkStyle: BOOLFALSE,
style: ATOMNIL,
styleProc: PROC [Ref] RETURNS [ATOM] ← NIL,
interrupt: REF BOOLNIL
]
RETURNS [found: BOOL, where: RefTextNode, at, atEnd, before, after: INT];
searches first node from start=0 to end=len
continues back through tree until finds a match or reaches last node
searches from lastStart in last node; searches from 0 in other nodes
Apply: PROC [
finder: Finder,
first: Ref,
proc: ApplyProc,
start: INT ← 0,
last: Ref ← NIL,
lastLen: INTLAST[INT],
looksExact: BOOLFALSE,
commentControl: CommentControl ← includeComments,
checkFormat: BOOLFALSE,
format: ATOMNIL,
checkStyle: BOOLFALSE,
style: ATOMNIL,
styleProc: PROC [Ref] RETURNS [ATOM] ← NIL
]
RETURNS
[count: LONG INTEGER];
finds a span of text and calls proc
ApplyProc: TYPE = PROC [where: RefTextNode, at, atEnd, before, after: INT]
RETURNS [continue, bumpCount: BOOL, from, delta: INT];
arg's are results from Try
if continue is true, Apply will Try again starting at "from" in "where"
delta tells change in length caused by the action
Apply does the following with delta
IF where=last AND lastLen < LAST[INT] THEN lastLen ← lastLen+delta
if bumpCount is true, Apply will increment its result counter
END.