DIRECTORY SpellingWordMap, Rope USING [ROPE, Size], RopeReader USING [Create, SetPosition, Get, Ref, Backwards]; SpellingWordMapImpl: CEDAR MONITOR IMPORTS Rope, RopeReader EXPORTS SpellingWordMap = BEGIN ROPE: TYPE = Rope.ROPE; reader: RopeReader.Ref _ RopeReader.Create[]; -- To make word parsing efficient. MapWordsInRope: PUBLIC ENTRY PROC [words: ROPE, buffer: REF TEXT, f: PROC [REF TEXT] RETURNS [BOOL]] RETURNS [premature: BOOLEAN _ FALSE, wordStart, wordEnd: INT _ 0, newBuffer: REF TEXT] = { HandleExpansion: PROC [] = INLINE { IF wp >= buffer.maxLength THEN { newBuffer _ NEW[TEXT[2*buffer.maxLength + 1]]; newBuffer.length _ newBuffer.maxLength; FOR j: NAT IN [0..wp) DO newBuffer[j] _ buffer[j]; ENDLOOP; buffer _ newBuffer; }; }; ParseState: TYPE = {InWord, InJunk, Apostrophe}; state: ParseState _ InJunk; wp: NAT _ 0; size: INT _ words.Size[]; c: CHAR; newBuffer _ buffer; buffer.length _ buffer.maxLength; RopeReader.SetPosition[reader, words, 0]; FOR pos: INT IN [0..size) DO c _ reader.Get[]; IF c IN ['a..'z] OR c IN ['A..'Z] THEN { IF state = InJunk THEN { wordStart _ pos; wp _ 0; } ELSE IF state = Apostrophe THEN { HandleExpansion[]; buffer[wp] _ '\'; wp _ wp + 1; }; state _ InWord; HandleExpansion[]; buffer[wp] _ c; wp _ wp + 1; } ELSE { IF state = InWord THEN { IF c = '\' THEN { state _ Apostrophe; } ELSE { buffer.length _ wp; wordEnd _ pos; premature _ f[buffer]; IF premature THEN RETURN; state _ InJunk; }; } ELSE IF state = Apostrophe THEN { buffer.length _ wp; wordEnd _ pos-1; premature _ f[buffer]; IF premature THEN RETURN; state _ InJunk; }; }; ENDLOOP; IF state = InWord THEN { buffer.length _ wp; wordEnd _ size; premature _ f[buffer]; } ELSE IF state = Apostrophe THEN { buffer.length _ wp; wordEnd _ size-1; premature _ f[buffer]; }; }; MapWordsInRopeBackward: PUBLIC ENTRY PROC [words: ROPE, buffer: REF TEXT, f: PROC [REF TEXT] RETURNS [BOOL]] RETURNS [premature: BOOLEAN _ FALSE, wordStart, wordEnd: INT _ 0, newBuffer: REF TEXT] = { HandleExpansion: PROC [] = INLINE { IF wp >= buffer.maxLength THEN { newBuffer _ NEW[TEXT[2*buffer.maxLength + 1]]; newBuffer.length _ newBuffer.maxLength; FOR j: NAT IN [0..wp) DO newBuffer[j] _ buffer[j]; ENDLOOP; buffer _ newBuffer; }; }; ReverseWord: PROC [] = INLINE { j: NAT _ buffer.length; c: CHAR; FOR i: NAT IN [0..j/2) DO j _ j - 1; c _ buffer[i]; buffer[i] _ buffer[j]; buffer[j] _ c; ENDLOOP; }; ParseState: TYPE = {InWord, InJunk, Apostrophe}; state: ParseState _ InJunk; wp: NAT _ 0; size: INT _ words.Size[]; c: CHAR; newBuffer _ buffer; buffer.length _ buffer.maxLength; RopeReader.SetPosition[reader, words, words.Size[]]; FOR pos: INT DECREASING IN [0..size) DO c _ reader.Backwards[]; IF c IN ['a..'z] OR c IN ['A..'Z] THEN { IF state = InJunk THEN { wordEnd _ pos+1; wp _ 0; } ELSE IF state = Apostrophe THEN { HandleExpansion[]; buffer[wp] _ '\'; wp _ wp + 1; }; state _ InWord; HandleExpansion[]; buffer[wp] _ c; wp _ wp + 1; } ELSE { IF state = InWord THEN { IF c = '\' THEN { state _ Apostrophe; } ELSE { buffer.length _ wp; wordStart _ pos+1; ReverseWord[]; premature _ f[buffer]; IF premature THEN RETURN; state _ InJunk; }; } ELSE IF state = Apostrophe THEN { buffer.length _ wp; wordStart _ pos+2; ReverseWord[]; premature _ f[buffer]; IF premature THEN RETURN; state _ InJunk; }; }; ENDLOOP; IF state = InWord THEN { buffer.length _ wp; wordStart _ 0; ReverseWord[]; premature _ f[buffer]; } ELSE IF state = Apostrophe THEN { buffer.length _ wp; wordStart _ 1; ReverseWord[]; premature _ f[buffer]; }; }; END. CHANGE LOG Created by Nix on October 7, 1983 2:22 pm pFile: SpellingWordMapImpl.mesa Last Edited by: Nix, October 24, 1983 10:47 am Maps the function f over each of the words in the ROPE words, stopping either when f returns TRUE or when all of the words are exhausted. A word is defined to be the longest non-empty sequence of ['A..'Z]+['a..'z], with embedded apostrophes allowed: "abcd" is a word, "ab'cd" is a word, but "abcd'" is not a word. The accumulation and testing of words is carried out in the buffer, which is grown to accomodate long words, and which is returned (possibly longer) when the mapping is complete. On return, premature is TRUE iff the function f returned TRUE on some word. If premature is true, then wordStart and wordEnd are set to the starting index and stopping index of the word for which f returned true. If premature is false, then these values denote nothing of interest. Maps the function f over each of the words in the ROPE words, stopping either when f returns TRUE or when all of the words are exhausted. A word is defined to be the longest non-empty sequence of ['A..'Z]+['a..'z], with embedded apostrophes allowed: "abcd" is a word, "ab'cd" is a word, but "abcd'" is not a word. The accumulation and testing of words is carried out in the buffer, which is grown to accomodate long words, and which is returned (possibly longer) when the mapping is complete. On return, premature is TRUE iff the function f returned TRUE on some word. If premature is true, then wordStart and wordEnd are set to the starting index and stopping index of the word for which f returned true. If premature is false, then these values denote nothing of interest. Κν˜– "Cedar" stylešœ™J™.—unitšΟk ˜ K˜Icodešœœœ˜Lšœ œ,˜<—KšΟbœœ˜"šœ˜Lšœ˜Lšœ ˜ —šœ˜Lšœ˜—Lš˜L˜Lšœœœ˜Jšœ.Οc"˜PJ˜J˜šžœ˜Jšœœœ œ œœœœœœœ˜TJš œ œœœœœ˜[Jšœ™šΟnœœœ˜#šœœ˜ Jšœ œœ˜.J˜'šœœœ ˜J˜Jšœ˜—J˜J˜—J˜—Jšœ œ ˜0J˜Jšœœ˜ Jšœœ˜Jšœœ˜J˜J˜!Jšœ)˜)šœœœ ˜J˜š œœ œœ œ˜(šœœ˜J˜J˜J˜—šœœœ˜!J˜J˜J˜ J˜—J˜J˜J˜J˜ J˜—šœ˜šœœ˜šœœ œ˜J˜J˜—šœ˜J˜Jšœ˜Jšœ˜Jšœ œœ˜J˜J˜—J˜—šœœœ˜!J˜J˜J˜Jšœ œœ˜J˜J˜—J˜—Jšœ˜—šœœ˜J˜J˜J˜J˜—šœœœ˜!J˜J˜J˜J˜—J˜J˜—šžœ˜Jšœœœ œ œœœœœœœ˜TJš œ œœœœœ˜[Jšœ™š œœœ˜#šœœ˜ Jšœ œœ˜.J˜'šœœœ ˜J˜Jšœ˜—J˜J˜—J˜—š  œœœ˜Jšœœ˜Jšœœ˜šœœœ ˜Jšœ ˜ Jšœ˜Jšœ˜J˜Jšœ˜—J˜—Jšœ œ ˜0J˜Jšœœ˜ Jšœœ˜Jšœœ˜J˜J˜!Jšœ4˜4šœœ œ ˜'Jšœ˜š œœ œœ œ˜(šœœ˜J˜J˜J˜—šœœœ˜!J˜J˜J˜ J˜—J˜J˜J˜J˜ J˜—šœ˜šœœ˜šœœ œ˜J˜J˜—šœ˜J˜Jšœ˜J˜Jšœ˜Jšœ œœ˜J˜J˜—J˜—šœœœ˜!J˜J˜J˜J˜Jšœ œœ˜J˜J˜—J˜—Jšœ˜—šœœ˜J˜J˜J˜J˜J˜—šœœœ˜!J˜J˜J˜J˜J˜—J˜J˜—šœ˜Kšœ˜ Kšœ)˜)——…—c