Skip to content

Commit ce992a2

Browse files
committed
add HasChunkRefs for Sentence and re-export extract helpers
1 parent f6a8fad commit ce992a2

File tree

2 files changed

+89
-73
lines changed

2 files changed

+89
-73
lines changed

code/drasil-lang/lib/Language/Drasil/Sentence.hs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1617
import Control.Lens ((^.))
1718
import Data.Char (toUpper)
1819

19-
import Drasil.Database (HasUID(..), UID)
20+
import Drasil.Database (HasChunkRefs(..), HasUID(..), UID)
2021

2122
import Language.Drasil.ExprClasses (Express(express))
2223
import Language.Drasil.ModelExpr.Lang (ModelExpr)
24+
import Language.Drasil.ModelExpr.Extract (meNames)
2325
import Language.Drasil.NounPhrase.Core (NP)
2426
import Language.Drasil.UnitLang (USymb)
2527
import 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).
2934
data SentenceStyle = PluralTerm
@@ -149,3 +154,81 @@ capSent x = x
149154
-- | Helper which creates a Header with size s of the 'Sentence'.
150155
headSent :: Int -> Sentence -> Sentence
151156
headSent 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 #-}
Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,7 @@
11
-- | Extract various kinds of UIDs from a Sentence. Used in conjunction with the
22
-- chunk database in order to render terms, symbols, and references properly.
3-
module Language.Drasil.Sentence.Extract(sdep, shortdep, lnames, lnames') where
3+
-- The actual traversals now live in 'Language.Drasil.Sentence', but this
4+
-- module keeps the old import path working.
5+
module Language.Drasil.Sentence.Extract (sdep, shortdep, lnames, lnames') where
46

5-
import Data.Containers.ListUtils (nubOrd)
6-
7-
import Drasil.Database (UID)
8-
9-
import Language.Drasil.Sentence(Sentence(..), SentenceStyle(..))
10-
import Language.Drasil.ModelExpr.Extract (meNames)
11-
12-
-- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's.
13-
getUIDs :: Sentence -> [UID]
14-
getUIDs (Ch ShortStyle _ _) = []
15-
getUIDs (Ch TermStyle _ _) = []
16-
getUIDs (Ch PluralTerm _ _) = []
17-
getUIDs (SyCh a) = [a]
18-
getUIDs Sy {} = []
19-
getUIDs NP {} = []
20-
getUIDs S {} = []
21-
getUIDs P {} = []
22-
getUIDs Ref {} = []
23-
getUIDs Percent = []
24-
getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b
25-
getUIDs (Quote a) = getUIDs a
26-
getUIDs (E a) = meNames a
27-
getUIDs EmptyS = []
28-
29-
-- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's
30-
-- but doesn't go into expressions.
31-
getUIDshort :: Sentence -> [UID]
32-
getUIDshort (Ch ShortStyle _ a) = [a]
33-
getUIDshort (Ch TermStyle _ _) = []
34-
getUIDshort (Ch PluralTerm _ _) = []
35-
getUIDshort SyCh {} = []
36-
getUIDshort Sy {} = []
37-
getUIDshort NP {} = []
38-
getUIDshort S {} = []
39-
getUIDshort Percent = []
40-
getUIDshort P {} = []
41-
getUIDshort Ref {} = []
42-
getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b
43-
getUIDshort (Quote a) = getUIDshort a
44-
getUIDshort E {} = []
45-
getUIDshort EmptyS = []
46-
47-
-----------------------------------------------------------------------------
48-
-- And now implement the exported traversals all in terms of the above
49-
-- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'.
50-
sdep :: Sentence -> [UID]
51-
sdep = nubOrd . getUIDs
52-
53-
-- This is to collect symbolic 'UID's that are printed out as an /abbreviation/.
54-
shortdep :: Sentence -> [UID]
55-
shortdep = nubOrd . getUIDshort
56-
57-
-- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's.
58-
lnames :: Sentence -> [UID]
59-
lnames Ch {} = []
60-
lnames SyCh {} = []
61-
lnames Sy {} = []
62-
lnames NP {} = []
63-
lnames S {} = []
64-
lnames Percent = []
65-
lnames P {} = []
66-
lnames (Ref a _ _) = [a]
67-
lnames ((:+:) a b) = lnames a ++ lnames b
68-
lnames Quote {} = []
69-
lnames E {} = []
70-
lnames EmptyS = []
71-
72-
-- | Get /reference/ 'UID's from 'Sentence's.
73-
lnames' :: [Sentence] -> [UID]
74-
lnames' = concatMap lnames
7+
import Language.Drasil.Sentence (sdep, shortdep, lnames, lnames')

0 commit comments

Comments
 (0)