Debian Codegraph @

Text/RE/ZeInternals/TestBench/Parsers.hs

raw
1 {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE CPP #-}
4
5 module Text.RE.ZeInternals.TestBench.Parsers
6 (
7 -- * The Test Bench
8 MacroEnv
9 , MacroDescriptor(..)
10 , RegexSource(..)
11 , WithCaptures(..)
12 , RegexType
13 , isTDFA
14 , isPCRE
15 , presentRegexType
16 -- ** Constructing a MacrosEnv
17 , mkMacros
18 -- ** Formatting Macros
19 , formatMacroTable
20 , formatMacroSummary
21 , formatMacroSources
22 , formatMacroSource
23 -- ** Formatting Macros
24 , testMacroEnv
25 , runTests
26 , runTests'
27 -- * Parsing
28 , parseInteger
29 , parseHex
30 , parseDouble
31 , parseString
32 , parseSimpleString
33 , parseDate
34 , parseSlashesDate
35 , parseTimeOfDay
36 , parseTimeZone
37 , parseDateTime
38 , parseDateTime8601
39 , parseDateTimeCLF
40 , parseShortMonth
41 , shortMonthArray
42 , IPV4Address
43 , parseIPv4Address
44 , Severity(..)
45 , parseSeverity
46 , severityKeywords
47 ) where
48
49 import Data.Array
50 import qualified Data.HashMap.Strict as HM
51 import Data.Maybe
52 import qualified Data.Text as T
53 import Data.Time
54 import qualified Data.Time.Locale.Compat as LC
55 import Data.Word
56 import Text.Printf
57 import Text.RE.Replace
58 import Text.RE.ZeInternals.TestBench
59 import Text.Read
60
61
62 parseInteger :: Replace a => a -> Maybe Int
63 parseInteger = readMaybe . unpackR
64
65 parseHex :: Replace a => a -> Maybe Int
66 parseHex = readMaybe . ("0x"++) . unpackR
67
68 parseDouble :: Replace a => a -> Maybe Double
69 parseDouble = readMaybe . unpackR
70
71 parseString :: Replace a => a -> Maybe T.Text
72 parseString = readMaybe . unpackR
73
74 parseSimpleString :: Replace a => a -> Maybe T.Text
75 parseSimpleString = Just . T.dropEnd 1 . T.drop 1 . textifyR
76
77 date_templates, time_templates, timezone_templates,
78 date_time_8601_templates, date_time_templates :: [String]
79 date_templates = ["%F"]
80 time_templates = ["%H:%M:%S","%H:%M:%S%Q","%H:%M"]
81 timezone_templates = ["Z","%z"]
82 date_time_8601_templates =
83 [ printf "%sT%s%s" dt tm tz
84 | dt <- date_templates
85 , tm <- time_templates
86 , tz <- timezone_templates
87 ]
88 date_time_templates =
89 [ printf "%s%c%s%s" dt sc tm tz
90 | dt <- date_templates
91 , sc <- ['T',' ']
92 , tm <- time_templates
93 , tz <- timezone_templates ++ [" UTC",""]
94 ]
95
96 parseDate :: Replace a => a -> Maybe Day
97 parseDate = parse_time date_templates
98
99 parseSlashesDate :: Replace a => a -> Maybe Day
100 parseSlashesDate = parse_time ["%Y/%m/%d"]
101
102 parseTimeOfDay :: Replace a => a -> Maybe TimeOfDay
103 parseTimeOfDay = parse_time time_templates
104
105 parseTimeZone :: Replace a => a -> Maybe TimeZone
106 parseTimeZone = parse_time timezone_templates
107
108 parseDateTime :: Replace a => a -> Maybe UTCTime
109 parseDateTime = parse_time date_time_templates
110
111 parseDateTime8601 :: Replace a => a -> Maybe UTCTime
112 parseDateTime8601 = parse_time date_time_8601_templates
113
114 parseDateTimeCLF :: Replace a => a -> Maybe UTCTime
115 parseDateTimeCLF = parse_time ["%d/%b/%Y:%H:%M:%S %z"]
116
117 parseShortMonth :: Replace a => a -> Maybe Int
118 parseShortMonth = flip HM.lookup short_month_hm . unpackR
119
120 parse_time :: (ParseTime t,Replace s) => [String] -> s -> Maybe t
121 parse_time tpls = prs . unpackR
122 where
123 prs s = listToMaybe $ catMaybes
124 [ parseTimeM True LC.defaultTimeLocale fmt s
125 | fmt<-tpls
126 ]
127 #if !MIN_VERSION_time(1,5,0)
128 parseTimeM _ = parseTime
129 #endif
130
131 short_month_hm :: HM.HashMap String Int
132 short_month_hm = HM.fromList [ (T.unpack $ shortMonthArray!i,i) | i<-[1..12] ]
133
134 shortMonthArray :: Array Int T.Text
135 shortMonthArray = listArray (1,12) $
136 T.words "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
137
138 type IPV4Address = (Word8,Word8,Word8,Word8)
139
140 parseIPv4Address :: Replace a => a -> Maybe IPV4Address
141 parseIPv4Address = prs . words_by (=='.') . unpackR
142 where
143 prs [a_s,b_s,c_s,d_s] = do
144 a <- readMaybe a_s
145 b <- readMaybe b_s
146 c <- readMaybe c_s
147 d <- readMaybe d_s
148 case all is_o [a,b,c,d] of
149 True -> Just (toEnum a,toEnum b,toEnum c,toEnum d)
150 False -> Nothing
151 prs _ = Nothing
152
153 is_o x = 0 <= x && x <= 255
154
155 data Severity
156 = Emerg
157 | Alert
158 | Crit
159 | Err
160 | Warning
161 | Notice
162 | Info
163 | Debug
164 deriving (Bounded,Enum,Ord,Eq,Show)
165
166 parseSeverity :: Replace a => a -> Maybe Severity
167 parseSeverity = flip HM.lookup severity_hm . textifyR
168
169 severity_hm :: HM.HashMap T.Text Severity
170 severity_hm = HM.fromList
171 [ (kw,pri)
172 | pri<-[minBound..maxBound]
173 , let (kw0,kws) = severityKeywords pri
174 , kw <- kw0:kws
175 ]
176
177 severityKeywords :: Severity -> (T.Text,[T.Text])
178 severityKeywords pri = case pri of
179 Emerg -> (,) "emerg" ["panic"]
180 Alert -> (,) "alert" []
181 Crit -> (,) "crit" []
182 Err -> (,) "err" ["error"]
183 Warning -> (,) "warning" ["warn"]
184 Notice -> (,) "notice" []
185 Info -> (,) "info" []
186 Debug -> (,) "debug" []
187
188 words_by :: (Char->Bool) -> String -> [String]
189 words_by f s = case dropWhile f s of
190 "" -> []
191 s' -> w : words_by f s''
192 where
193 (w, s'') = break f s'