@@ -10,20 +10,25 @@ module Language.Drasil.Sentence (
1010 -- * Functions
1111 (+:+) , (+:+.) , (+:) , (!.) , capSent , headSent , ch , eS , eS' , sC , sDash , sParen ,
1212 sentencePlural , sentenceShort ,
13- sentenceSymb , sentenceTerm
13+ sentenceSymb , sentenceTerm ,
14+ sdep , shortdep , lnames , lnames'
1415) where
1516
1617import Control.Lens ((^.) )
1718import Data.Char (toUpper )
1819
19- import Drasil.Database (HasUID (.. ), UID )
20+ import Drasil.Database (HasChunkRefs ( .. ), HasUID (.. ), UID )
2021
2122import Language.Drasil.ExprClasses (Express (express ))
2223import Language.Drasil.ModelExpr.Lang (ModelExpr )
24+ import Language.Drasil.ModelExpr.Extract (meNames )
2325import Language.Drasil.NounPhrase.Core (NP )
2426import Language.Drasil.UnitLang (USymb )
2527import Language.Drasil.Symbol (HasSymbol , Symbol )
2628
29+ import Data.Containers.ListUtils (nubOrd )
30+ import qualified Data.Set as Set
31+
2732-- | Used in 'Ch' constructor to determine the state of a term
2833-- (can record whether something is in plural form, a singular term, or in short form).
2934data SentenceStyle = PluralTerm
@@ -149,3 +154,81 @@ capSent x = x
149154-- | Helper which creates a Header with size s of the 'Sentence'.
150155headSent :: Int -> Sentence -> Sentence
151156headSent s x = S (concat (replicate s " #" )) :+: S " " :+: x
157+
158+ -- | Helpers for extracting references -----------------------------------------
159+
160+ -- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's.
161+ getUIDs :: Sentence -> [UID ]
162+ getUIDs (Ch ShortStyle _ _) = []
163+ getUIDs (Ch TermStyle _ _) = []
164+ getUIDs (Ch PluralTerm _ _) = []
165+ getUIDs (SyCh a) = [a]
166+ getUIDs Sy {} = []
167+ getUIDs NP {} = []
168+ getUIDs S {} = []
169+ getUIDs P {} = []
170+ getUIDs Ref {} = []
171+ getUIDs Percent = []
172+ getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b
173+ getUIDs (Quote a) = getUIDs a
174+ getUIDs (E a) = meNames a
175+ getUIDs EmptyS = []
176+
177+ -- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's
178+ -- but doesn't go into expressions.
179+ getUIDshort :: Sentence -> [UID ]
180+ getUIDshort (Ch ShortStyle _ a) = [a]
181+ getUIDshort (Ch TermStyle _ _) = []
182+ getUIDshort (Ch PluralTerm _ _) = []
183+ getUIDshort SyCh {} = []
184+ getUIDshort Sy {} = []
185+ getUIDshort NP {} = []
186+ getUIDshort S {} = []
187+ getUIDshort Percent = []
188+ getUIDshort P {} = []
189+ getUIDshort Ref {} = []
190+ getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b
191+ getUIDshort (Quote a) = getUIDshort a
192+ getUIDshort E {} = []
193+ getUIDshort EmptyS = []
194+
195+ -----------------------------------------------------------------------------
196+ -- And now implement the exported traversals all in terms of the above
197+ -- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'.
198+ sdep :: Sentence -> [UID ]
199+ sdep = nubOrd . getUIDs
200+ {-# INLINE sdep #-}
201+
202+ -- This is to collect symbolic 'UID's that are printed out as an /abbreviation/.
203+ shortdep :: Sentence -> [UID ]
204+ shortdep = nubOrd . getUIDshort
205+ {-# INLINE shortdep #-}
206+
207+ -- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's.
208+ lnames :: Sentence -> [UID ]
209+ lnames Ch {} = []
210+ lnames SyCh {} = []
211+ lnames Sy {} = []
212+ lnames NP {} = []
213+ lnames S {} = []
214+ lnames Percent = []
215+ lnames P {} = []
216+ lnames (Ref a _ _) = [a]
217+ lnames ((:+:) a b) = lnames a ++ lnames b
218+ lnames Quote {} = []
219+ lnames E {} = []
220+ lnames EmptyS = []
221+ {-# INLINE lnames #-}
222+
223+ -- | Get /reference/ 'UID's from 'Sentence's.
224+ lnames' :: [Sentence ] -> [UID ]
225+ lnames' = concatMap lnames
226+ {-# INLINE lnames' #-}
227+
228+ sentenceRefs :: Sentence -> Set. Set UID
229+ sentenceRefs sent = Set. fromList (lnames sent ++ sdep sent ++ shortdep sent)
230+ {-# INLINE sentenceRefs #-}
231+
232+ instance HasChunkRefs Sentence where
233+ chunkRefs = sentenceRefs
234+ {-# INLINABLE chunkRefs #-}
0 commit comments