<> <> <> <> <> <> <> <<>> DIRECTORY LooksReader USING [Create, Get, SetPosition], Rope USING [Cat, Equal, Fetch, FromChar, Map, ROPE, Size, Substr], RopeEdit USING [AlphaNumericChar, BlankChar, LowerCase, MaxLen, Offset, UpperCase], RopeReader USING [Create, Get, GetIndex, GetRope, Peek, ReadOffEnd, SetPosition], RunReader USING [Create, Get, GetIndex, GetRuns, NoMoreRuns, SetPosition], TextEdit USING [GetRope, GetRuns], TextFind USING [PatternErrorCode, RefTextNode], TextFindPrivate USING [anyAlphaPattern, anyBlankPattern, anyNonAlphaPattern, anyNonBlankPattern, anyStringPattern, FinderRecord, leftBoundaryPattern, leftBracketPattern, LooksArray, maxAlphaPattern, maxBlankPattern, maxNonAlphaPattern, maxNonBlankPattern, MaxPatternLength, maxStringPattern, NameArray, nopPattern, oneAlphaPattern, oneBlankPattern, oneCharPattern, oneNonAlphaPattern, oneNonBlankPattern, PatternArray, PatternStackArray, rightBoundaryPattern, rightBracketPattern, TextStackArray], TextLooks USING [FetchLooks, Looks, noLooks, Runs], TextLooksSupport USING [], TextNode USING [pZone]; TextFindImpl: CEDAR PROGRAM IMPORTS TextEdit, TextLooks, TextLooksSupport, LooksReader, RopeEdit, RopeReader, TextNode, RunReader, Rope EXPORTS TextFind = { OPEN TextFind; ROPE: TYPE ~ Rope.ROPE; MalformedPattern: PUBLIC ERROR [ec:PatternErrorCode] = CODE; Finder: TYPE = REF FinderRec; FinderRec: PUBLIC TYPE = TextFindPrivate.FinderRecord; <<***** Operations *****>> NameLoc: PUBLIC PROC [finder: Finder, name: ROPE] RETURNS [at, atEnd: RopeEdit.Offset] = { OPEN finder; at _ atEnd _ 0; IF finder = NIL OR nameArray = NIL THEN RETURN; FOR i:NAT IN [0..nameArray.length) DO IF Rope.Equal[nameArray[i].name, name] THEN RETURN [nameArray[i].at,nameArray[i].atEnd]; ENDLOOP; }; NameLooks: PUBLIC PROC [finder: Finder, name: ROPE] RETURNS [looks: TextLooks.Looks] = { OPEN finder; looks _ TextLooks.noLooks; IF finder = NIL OR nameArray = NIL THEN RETURN; FOR i:NAT IN [0..nameArray.length) DO IF Rope.Equal[nameArray[i].name, name] THEN RETURN [nameArray[i].looks]; ENDLOOP; }; Create: PUBLIC PROC [pattern: RefTextNode, literal, word, ignoreLooks, ignoreCase, addBounds: BOOLEAN _ FALSE, patternStart: RopeEdit.Offset _ 0, patternLen: RopeEdit.Offset _ RopeEdit.MaxLen] RETURNS [finder: Finder] = { patternRope: ROPE _ TextEdit.GetRope[pattern]; patternRuns: TextLooks.Runs _ TextEdit.GetRuns[pattern]; RETURN [CreateFromParts[patternRope,patternRuns,literal,word, ignoreLooks,ignoreCase,addBounds,patternStart,patternLen]] }; CreateFromRope: PUBLIC PROC [ pattern: ROPE, literal, word, ignoreCase, addBounds: BOOLEAN _ FALSE, patternStart: RopeEdit.Offset _ 0, patternLen: RopeEdit.Offset _ RopeEdit.MaxLen] RETURNS [finder: Finder] = { RETURN [CreateFromParts[pattern,NIL,literal,word,TRUE, ignoreCase,addBounds,patternStart,patternLen]] }; CreateFromParts: PROC [patternRope: ROPE, patternRuns: TextLooks.Runs, literal, word, ignoreLooks, ignoreCase, addBounds: BOOLEAN _ FALSE, patternStart: RopeEdit.Offset _ 0, patternLen: RopeEdit.Offset _ RopeEdit.MaxLen] RETURNS [finder: Finder] = { NewLooks: PROC [num: NAT] RETURNS [array: REF TextFindPrivate.LooksArray] = { array _ TextNode.pZone.NEW[TextFindPrivate.LooksArray[num]]; FOR i:NAT IN [0..num) DO array[i] _ TextLooks.noLooks; ENDLOOP }; char, patternChar: CHAR _ 377C; pLen: RopeEdit.Offset; patternLength, plen, psIndex, nameCount: NAT _ 0; nameList: LIST OF Rope.ROPE; -- in reverse order of appearance nameLooksList: LIST OF TextLooks.Looks; nameLooks: TextLooks.Looks; insideNamedPat: BOOLEAN _ FALSE; IF addBounds THEN { -- add |'s to both ends of pattern IF literal THEN { -- put quotes before special chars in the pattern new: Rope.ROPE; AddQuotes: SAFE PROC [c: CHAR] RETURNS [stop: BOOL] = TRUSTED { IF ~RopeEdit.BlankChar[c] AND ~RopeEdit.AlphaNumericChar[c] THEN new _ Rope.Cat[new, "'"]; -- quote chars that are not blank or alpha or digit new _ Rope.Cat[new, Rope.FromChar[c]]; RETURN [FALSE] }; [] _ Rope.Map[base: patternRope, action: AddQuotes]; patternRope _ new; literal _ FALSE; }; patternRope _ Rope.Cat["|", Rope.Cat[patternRope, "|"]]; }; pLen _ Rope.Size[patternRope]; patternStart _ MIN[patternStart,pLen]; IF (patternLen _ MIN[patternLen,pLen-patternStart]) > TextFindPrivate.MaxPatternLength THEN ERROR MalformedPattern[toobig]; patternLength _ plen _ patternLen; finder _ TextNode.pZone.NEW[FinderRec]; { OPEN finder; PatternProc: TYPE = PROC [char: CHAR, looks: TextLooks.Looks, ignoreCase: BOOLEAN]; patProc: PatternProc = IF literal THEN LitChar ELSE PatChar; GetLooks: PROC RETURNS [lks: TextLooks.Looks] = { RETURN [IF lksReader = NIL THEN TextLooks.noLooks ELSE LooksReader.Get[lksReader ! RunReader.NoMoreRuns => { lks _ TextLooks.noLooks; CONTINUE }]] }; LitChar: PatternProc = TRUSTED { IF looks # TextLooks.noLooks THEN { IF patternLooks = NIL THEN patternLooks _ NewLooks[patternLength]; patternLooks[psIndex] _ looks; }; IF ignoreCase AND (char IN ['A..'Z] OR char IN ['a..'z]) THEN { patternArray[psIndex] _ [pattern[char+200B]]; <<200B tells matcher to check both upper and lower>> IF psIndex = 0 THEN { firstPatternCharIsNormal _ TRUE; firstPatChar1 _ RopeEdit.UpperCase[char]; firstPatChar2 _ RopeEdit.LowerCase[char]; }; IF psIndex = patternLength-1 THEN { lastPatternCharIsNormal _ TRUE; lastPatChar1 _ RopeEdit.UpperCase[char]; lastPatChar2 _ RopeEdit.LowerCase[char]; } } ELSE { patternArray[psIndex] _ [pattern[char]]; IF psIndex = patternLength-1 THEN { lastPatternCharIsNormal _ TRUE; lastPatChar1 _ char; lastPatChar2 _ 0C; }; IF psIndex = 0 THEN { firstPatternCharIsNormal _ TRUE; firstPatChar1 _ char; firstPatChar2 _ 0C; } } }; NotChar: PatternProc = TRUSTED { IF looks # TextLooks.noLooks THEN { IF patternLooks = NIL THEN patternLooks _ NewLooks[patternLength]; patternLooks[psIndex] _ looks; }; IF ignoreCase THEN patternArray[psIndex] _ [not[char+200B]] ELSE patternArray[psIndex] _ [not[char]]; }; PatChar: PatternProc = TRUSTED { IF looks # TextLooks.noLooks AND patternLooks = NIL THEN patternLooks _ NewLooks[patternLength]; IF patternLooks # NIL THEN patternLooks[psIndex] _ looks; SELECT char FROM '' => { IF RopeReader.GetIndex[ropeReader] >= plen THEN ERROR MalformedPattern[endquote]; patternLength _ patternLength-1; LitChar[RopeReader.Get[ropeReader],GetLooks[],FALSE] }; IN ['A .. 'Z], IN ['a .. 'z] => LitChar[char,looks,ignoreCase]; '~ => { IF RopeReader.GetIndex[ropeReader] >= plen THEN ERROR MalformedPattern[endtilda]; patternLength _ patternLength-1; char _ RopeReader.Get[ropeReader]; looks _ GetLooks[]; SELECT char FROM '' => { IF RopeReader.GetIndex[ropeReader] >= plen THEN ERROR MalformedPattern[endquote]; patternLength _ patternLength-1; NotChar[RopeReader.Get[ropeReader],GetLooks[],FALSE] }; IN ['A .. 'Z], IN ['a .. 'z] => NotChar[char,looks,ignoreCase]; '% => patternArray[psIndex] _ [pattern[TextFindPrivate.oneNonBlankPattern]]; '$ => IF psIndex > 0 AND patternArray[psIndex-1]=[pattern[TextFindPrivate.anyNonBlankPattern]] AND (patternLooks=NIL OR patternLooks[psIndex-1]=looks) THEN { -- change to max patternLength _ patternLength-1; psIndex _ psIndex-1; patternArray[psIndex] _ [pattern[TextFindPrivate.maxNonBlankPattern]]; } ELSE { -- new entry patternArray[psIndex] _ [pattern[TextFindPrivate.anyNonBlankPattern]]; stackSize _ stackSize+1; }; '@ => patternArray[psIndex] _ [pattern[TextFindPrivate.oneNonAlphaPattern]]; '& => IF psIndex > 0 AND patternArray[psIndex-1]=[pattern[TextFindPrivate.anyNonAlphaPattern]] AND (patternLooks=NIL OR patternLooks[psIndex-1]=looks) THEN { -- change to max patternLength _ patternLength-1; psIndex _ psIndex-1; patternArray[psIndex] _ [pattern[TextFindPrivate.maxNonAlphaPattern]] } ELSE { -- new entry patternArray[psIndex] _ [pattern[TextFindPrivate.anyNonAlphaPattern]]; stackSize _ stackSize+1; }; ENDCASE => patternArray[psIndex] _ [not[char]]; }; '# => patternArray[psIndex] _ [pattern[TextFindPrivate.oneCharPattern]]; '* => IF psIndex > 0 AND patternArray[psIndex-1]=[pattern[TextFindPrivate.anyStringPattern]] AND (patternLooks=NIL OR patternLooks[psIndex-1]=looks) THEN { -- change to max psIndex _ psIndex-1; patternLength _ patternLength-1; stackSize _ MAX[1, stackSize]; patternArray[psIndex] _ [pattern[TextFindPrivate.maxStringPattern]]; } ELSE { -- new entry patternArray[psIndex] _ [pattern[TextFindPrivate.anyStringPattern]]; IF looks # TextLooks.noLooks THEN stackSize _ stackSize+1; }; '% => patternArray[psIndex] _ [pattern[TextFindPrivate.oneBlankPattern]]; '$ => IF psIndex > 0 AND patternArray[psIndex-1]=[pattern[TextFindPrivate.anyBlankPattern]] AND (patternLooks=NIL OR patternLooks[psIndex-1]=looks) THEN { -- change to max patternLength _ patternLength-1; psIndex _ psIndex-1; patternArray[psIndex] _ [pattern[TextFindPrivate.maxBlankPattern]] } ELSE { -- new entry patternArray[psIndex] _ [pattern[TextFindPrivate.anyBlankPattern]]; stackSize _ stackSize+1 }; '@ => patternArray[psIndex] _ [pattern[TextFindPrivate.oneAlphaPattern]]; '& => IF psIndex > 0 AND patternArray[psIndex-1]=[pattern[TextFindPrivate.anyAlphaPattern]] AND (patternLooks=NIL OR patternLooks[psIndex-1]=looks) THEN { -- change to max patternLength _ patternLength-1; psIndex _ psIndex-1; patternArray[psIndex] _ [pattern[TextFindPrivate.maxAlphaPattern]] } ELSE { -- new entry patternArray[psIndex] _ [pattern[TextFindPrivate.anyAlphaPattern]]; stackSize _ stackSize+1; }; '| => { patternArray[psIndex] _ [pattern[IF psIndex = 0 THEN TextFindPrivate.leftBoundaryPattern ELSE TextFindPrivate.rightBoundaryPattern]]; IF psIndex # 0 AND psIndex # patternLength-1 THEN ERROR MalformedPattern[boundary]; IF psIndex = patternLength-1 THEN { -- right boundary lastPatternCharIsNormal _ TRUE; lastPatChar1 _ TextFindPrivate.rightBoundaryPattern; }; IF psIndex = 0 THEN { --left boundary firstPatternCharIsNormal _ TRUE; firstPatChar1 _ TextFindPrivate.leftBoundaryPattern; } }; '< => { nameStart: RopeEdit.Offset _ RopeReader.GetIndex[ropeReader]; -- index of char after the < nameLen: RopeEdit.Offset _ 0; IF insideNamedPat THEN ERROR MalformedPattern[missingNameEnd]; insideNamedPat _ TRUE; nameLooks _ looks; -- remember the looks of the < patternArray[psIndex] _ [startname[nameCount]]; DO SELECT RopeReader.Peek[ropeReader ! RopeReader.ReadOffEnd => GOTO BadName] FROM -- scan to end of name ': => { -- pattern follows [] _ RopeReader.Get[ropeReader]; [] _ GetLooks[]; patternLength _ patternLength-(nameLen+1); EXIT; }; '> => { -- no pattern given, so insert a phony * psIndex _ psIndex + 1; PatChar['*,looks,ignoreCase]; -- use looks from the '< patternLength _ patternLength-nameLen+1; EXIT; }; ENDCASE => { -- part of the name nameLen _ nameLen+1; [] _ RopeReader.Get[ropeReader]; [] _ GetLooks[]; }; ENDLOOP; nameList _ TextNode.pZone.CONS[Rope.Substr[patternRope,nameStart,nameLen],nameList]; nameLooksList _ TextNode.pZone.CONS[nameLooks,nameLooksList]; EXITS BadName => ERROR MalformedPattern[missingNameEnd] }; '> => { IF ~insideNamedPat THEN ERROR MalformedPattern[unmatchedNameEnd]; insideNamedPat _ FALSE; patternArray[psIndex] _ [endname[nameCount]]; nameCount _ nameCount+1; }; '{ => { leftBracketSeen _ TRUE; patternArray[psIndex] _ [pattern[TextFindPrivate.leftBracketPattern]]; }; '} => { IF rightBracketSeen THEN patternArray[psIndex] _ [pattern[TextFindPrivate.nopPattern]] -- use first } in pattern ELSE { rightBracketSeen _ TRUE; patternArray[psIndex] _ [pattern[TextFindPrivate.rightBracketPattern]] } }; ENDCASE => { patternArray[psIndex] _ [pattern[char]]; IF psIndex = patternLength-1 THEN { lastPatternCharIsNormal _ TRUE; lastPatChar1 _ char; lastPatChar2 _ 0C }; IF psIndex = 0 THEN { firstPatternCharIsNormal _ TRUE; firstPatChar1 _ char; firstPatChar2 _ 0C } } }; -- end of PatChar IF word THEN wordSearch _ TRUE <> ELSE IF patternLength=2 AND ~literal AND ~ignoreLooks AND Rope.Fetch[patternRope,patternStart]='# AND Rope.Fetch[patternRope,patternStart+1]='* THEN { -- for looks-only searches looks _ TextLooks.FetchLooks[patternRuns,patternStart]; looksOnly _ TRUE; runReader _ RunReader.Create[]; RETURN; }; patternArray _ TextNode.pZone.NEW[TextFindPrivate.PatternArray[patternLength]]; ropeReader _ RopeReader.Create[]; RopeReader.SetPosition[ropeReader,patternRope,patternStart]; IF patternRuns # NIL AND ~ignoreLooks THEN { lksReader _ LooksReader.Create[]; LooksReader.SetPosition[lksReader,patternRuns,patternStart]; }; psIndex _ 0; DO -- unpack the pattern char: CHAR _ RopeReader.Get[ropeReader ! RopeReader.ReadOffEnd => EXIT]; patProc[char,GetLooks[],ignoreCase]; psIndex _ psIndex + 1; ENDLOOP; IF insideNamedPat THEN ERROR MalformedPattern[missingNameEnd]; -- mfp length _ patternLength; IF stackSize > 0 THEN { stackSize _ stackSize+1; textPosStack _ TextNode.pZone.NEW[TextFindPrivate.TextStackArray[stackSize]]; textLenStack _ TextNode.pZone.NEW[TextFindPrivate.TextStackArray[stackSize]]; patternPosStack _ TextNode.pZone.NEW[TextFindPrivate.PatternStackArray[stackSize]]; }; IF nameList # NIL THEN { nameArray _ TextNode.pZone.NEW[TextFindPrivate.NameArray[nameCount]]; FOR i:NAT DECREASING IN [0..nameCount) DO nameArray[i].name _ nameList.first; nameArray[i].looks _ nameLooksList.first; nameList _ nameList.rest; nameLooksList _ nameLooksList.rest; ENDLOOP; }; }; -- of OPEN finder }; -- of Create Start: PUBLIC PROC = {}; }. <s.>>