1module Data.Schematic.Verifier.Text where
6import Data.Schematic.Verifier.Common
7import Data.Text (Text, unpack)
8import Text.Regex.TDFA.Pattern
9import Text.Regex.TDFA.ReadRegex (parseRegex)
11toStrictTextLength :: [DemotedTextConstraint] -> [DemotedTextConstraint]
12toStrictTextLength = map f
14 f (DTLe x) = DTLt (x + 1)
15 f (DTGe x) = DTGt (x - 1)
18data VerifiedTextConstraint
20 | VTBounds (Maybe Integer)
28verifyTextLengthConstraints ::
29 [DemotedTextConstraint] -> Maybe (Maybe VerifiedTextConstraint)
30verifyTextLengthConstraints cs' = do
31 let cs = toStrictTextLength cs'
32 mlt = simplifyDNLs [x | DTLt x <- cs]
33 mgt = simplifyDNGs [x | DTGt x <- cs]
34 meq <- verifyDNEq [x | DTEq x <- cs]
35 verifyEquations mgt meq mlt
36 case all isNothing ([mgt, meq, mlt] :: [Maybe Integer]) of
43 Nothing -> VTBounds mgt mlt
45regexLength :: Text -> Maybe (Int, Maybe Int)
47 case parseRegex . unpack $ regexp of
48 Right (p, _) -> Just (minRegexLength p, maxRegexLength p)
51minRegexLength :: Pattern -> Int
59 PPlus sch -> minRegexLength $ PBound 1 Nothing sch
60 PStar _ sch -> minRegexLength $ PBound 0 Nothing sch
61 PBound low _ sch -> low * minRegexLength sch
62 PConcat ps -> sum $ fmap minRegexLength ps
63 POr xs -> minimum $ fmap minRegexLength xs
70maxRegexLength :: Pattern -> Maybe Int
80 PBound _ mhigh sch -> (*) <$> mhigh <*> maxRegexLength sch
81 PConcat ps -> sum <$> mapM maxRegexLength ps
82 POr xs -> maximum <$> mapM maxRegexLength xs
89verifyTextRegexConstraint ::
90 [DemotedTextConstraint] -> Maybe (Maybe VerifiedTextConstraint)
91verifyTextRegexConstraint cs = do
92 let regexps = [x | DTRegex x <- cs]
96 (l, mh) <- regexLength x
97 Just $ Just $ VTRegex x (fromIntegral l) (fromIntegral <$> mh)
100verifyTextEnumConstraint ::
101 [DemotedTextConstraint] -> Maybe (Maybe VerifiedTextConstraint)
102verifyTextEnumConstraint cs = do
103 let enums = concat [x | DTEnum x <- cs]
106 x -> Just $ Just $ VTEnum x
108verifyTextConstraints ::
109 [DemotedTextConstraint] -> Maybe [VerifiedTextConstraint]
110verifyTextConstraints cs = do
111 regexp <- verifyTextRegexConstraint cs
112 void $ case regexp of
113 Just (VTRegex _ l mh) ->
114 verifyTextLengthConstraints (DTGe l : cs ++ maybeToList (DTLe <$> mh))
116 lengths <- verifyTextLengthConstraints cs
117 enums <- verifyTextEnumConstraint cs
118 return $ catMaybes [lengths, enums, regexp]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list