diff options
Diffstat (limited to 'src/Data/JLD/Model/Keyword.hs')
-rw-r--r-- | src/Data/JLD/Model/Keyword.hs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/Data/JLD/Model/Keyword.hs b/src/Data/JLD/Model/Keyword.hs new file mode 100644 index 0000000..10835a9 --- /dev/null +++ b/src/Data/JLD/Model/Keyword.hs | |||
@@ -0,0 +1,135 @@ | |||
1 | module Data.JLD.Model.Keyword ( | ||
2 | Keyword (..), | ||
3 | parseKeyword, | ||
4 | isKeyword, | ||
5 | isNotKeyword, | ||
6 | allKeywords, | ||
7 | isKeywordLike, | ||
8 | ) where | ||
9 | |||
10 | import Data.JLD.Prelude hiding (show) | ||
11 | |||
12 | import Data.Char (isAlpha) | ||
13 | import Data.Foldable qualified as F | ||
14 | import Data.Text qualified as T (all, null, uncons) | ||
15 | import Text.Show (Show (..)) | ||
16 | |||
17 | data Keyword | ||
18 | = KeywordAny | ||
19 | | KeywordBase | ||
20 | | KeywordContainer | ||
21 | | KeywordContext | ||
22 | | KeywordDefault | ||
23 | | KeywordDirection | ||
24 | | KeywordEmbed | ||
25 | | KeywordExplicit | ||
26 | | KeywordFirst | ||
27 | | KeywordGraph | ||
28 | | KeywordId | ||
29 | | KeywordImport | ||
30 | | KeywordIncluded | ||
31 | | KeywordIndex | ||
32 | | KeywordJson | ||
33 | | KeywordLanguage | ||
34 | | KeywordList | ||
35 | | KeywordNest | ||
36 | | KeywordNone | ||
37 | | KeywordNull | ||
38 | | KeywordOmitDefault | ||
39 | | KeywordPrefix | ||
40 | | KeywordPreserve | ||
41 | | KeywordPropagate | ||
42 | | KeywordProtected | ||
43 | | KeywordRequireAll | ||
44 | | KeywordReverse | ||
45 | | KeywordSet | ||
46 | | KeywordType | ||
47 | | KeywordValue | ||
48 | | KeywordVersion | ||
49 | | KeywordVocab | ||
50 | deriving (Eq, Ord) | ||
51 | |||
52 | instance Show Keyword where | ||
53 | show = \case | ||
54 | KeywordAny -> "@any" | ||
55 | KeywordBase -> "@base" | ||
56 | KeywordContainer -> "@container" | ||
57 | KeywordContext -> "@context" | ||
58 | KeywordDefault -> "@default" | ||
59 | KeywordDirection -> "@direction" | ||
60 | KeywordEmbed -> "@embed" | ||
61 | KeywordExplicit -> "@explicit" | ||
62 | KeywordFirst -> "@first" | ||
63 | KeywordGraph -> "@graph" | ||
64 | KeywordId -> "@id" | ||
65 | KeywordImport -> "@import" | ||
66 | KeywordIncluded -> "@included" | ||
67 | KeywordIndex -> "@index" | ||
68 | KeywordJson -> "@json" | ||
69 | KeywordLanguage -> "@language" | ||
70 | KeywordList -> "@list" | ||
71 | KeywordNest -> "@nest" | ||
72 | KeywordNone -> "@none" | ||
73 | KeywordNull -> "@null" | ||
74 | KeywordOmitDefault -> "@omitDefault" | ||
75 | KeywordPrefix -> "@prefix" | ||
76 | KeywordPreserve -> "@preserve" | ||
77 | KeywordPropagate -> "@propagate" | ||
78 | KeywordProtected -> "@protected" | ||
79 | KeywordRequireAll -> "@requireAll" | ||
80 | KeywordReverse -> "@reverse" | ||
81 | KeywordSet -> "@set" | ||
82 | KeywordType -> "@type" | ||
83 | KeywordValue -> "@value" | ||
84 | KeywordVersion -> "@version" | ||
85 | KeywordVocab -> "@vocab" | ||
86 | |||
87 | parseKeyword :: Text -> Maybe Keyword | ||
88 | parseKeyword = \case | ||
89 | "@any" -> Just KeywordAny | ||
90 | "@base" -> Just KeywordBase | ||
91 | "@container" -> Just KeywordContainer | ||
92 | "@context" -> Just KeywordContext | ||
93 | "@default" -> Just KeywordDefault | ||
94 | "@direction" -> Just KeywordDirection | ||
95 | "@embed" -> Just KeywordEmbed | ||
96 | "@explicit" -> Just KeywordExplicit | ||
97 | "@first" -> Just KeywordFirst | ||
98 | "@graph" -> Just KeywordGraph | ||
99 | "@id" -> Just KeywordId | ||
100 | "@import" -> Just KeywordImport | ||
101 | "@included" -> Just KeywordIncluded | ||
102 | "@index" -> Just KeywordIndex | ||
103 | "@json" -> Just KeywordJson | ||
104 | "@language" -> Just KeywordLanguage | ||
105 | "@list" -> Just KeywordList | ||
106 | "@nest" -> Just KeywordNest | ||
107 | "@none" -> Just KeywordNone | ||
108 | "@null" -> Just KeywordNull | ||
109 | "@omitDefault" -> Just KeywordOmitDefault | ||
110 | "@prefix" -> Just KeywordPrefix | ||
111 | "@preserve" -> Just KeywordPreserve | ||
112 | "@propagate" -> Just KeywordPropagate | ||
113 | "@protected" -> Just KeywordProtected | ||
114 | "@requireAll" -> Just KeywordRequireAll | ||
115 | "@reverse" -> Just KeywordReverse | ||
116 | "@set" -> Just KeywordSet | ||
117 | "@type" -> Just KeywordType | ||
118 | "@value" -> Just KeywordValue | ||
119 | "@version" -> Just KeywordVersion | ||
120 | "@vocab" -> Just KeywordVocab | ||
121 | _ -> Nothing | ||
122 | |||
123 | isKeyword :: Foldable f => Text -> f Keyword -> Bool | ||
124 | isKeyword (parseKeyword -> Just keyword) (F.elem keyword -> True) = True | ||
125 | isKeyword _ _ = False | ||
126 | |||
127 | isNotKeyword :: Foldable f => Text -> f Keyword -> Bool | ||
128 | isNotKeyword s = isKeyword s .> not | ||
129 | |||
130 | allKeywords :: Foldable f => f Text -> f Keyword -> Bool | ||
131 | allKeywords values keywords = all (`isKeyword` keywords) values | ||
132 | |||
133 | isKeywordLike :: Text -> Bool | ||
134 | isKeywordLike (T.uncons -> Just ('@', res)) = not (T.null res) && T.all isAlpha res | ||
135 | isKeywordLike _ = False | ||