-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.hs
129 lines (98 loc) · 3.68 KB
/
Helpers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
module Helpers where
import Language
import Text.ParserCombinators.Parsec
import Data.Colour.SRGB
import Data.Colour.Names
import Data.Functor.Identity
import Data.Maybe (fromMaybe)
import qualified Text.Parsec.Prim as P
import qualified DAST as DAST
----
---- helpers
----
upgradeNames :: Maybe DAST.Upgrades -> [String]
upgradeNames Nothing = []
upgradeNames (Just u) = s' ++ d' ++ b'
where
s' = map DAST.getShotUpgradeName $ DAST.getShotUpgrades u
d' = map DAST.getShieldName $ DAST.getShieldUpgrades u
b' = map DAST.getBombName $ DAST.getBombUpgrades u
singleNames :: DAST.NEList DAST.Single -> [String]
singleNames = map DAST.getSingleName . toList
groupNames :: [DAST.Group] -> [String]
groupNames = map DAST.getGroupName
randomNames :: [DAST.Random] -> [String]
randomNames = map DAST.getRandomName
timelineNames :: [DAST.Random] -> [DAST.Group] -> DAST.NEList DAST.Single -> DAST.Elements -> ([String], [String], [String], [String])
timelineNames r g s u = (r',g',s',u')
where
r' = randomNames r
g' = groupNames g
s' = singleNames s
u' = upgradeNames $ DAST.getUpgrades u
-- quote definition
quote :: Parser String
quote = string "\""
toList :: DAST.NEList a -> [a]
toList (DAST.NEList x xs) = x : xs
antagNames :: DAST.Elements -> [String]
antagNames = map DAST.getAntagName . toList . DAST.getAntags
bulletNames :: DAST.Elements -> [String]
bulletNames = map DAST.getBulletName . DAST.getBullets
str :: Parser String
str = try $ do
s <- between quote quote (many alphaNum)
spaces
return $ s
natify :: String -> DAST.Nat
natify = DAST.toNat . read
readColor :: String -> Colour Double
readColor color = fromMaybe (error $ "invalid color: " ++ color) $ readColourName color
makeColor :: String -> DAST.Colour
makeColor = DAST.Colour . sRGB24show . readColor
-- get line
line :: Parser String
line = do
ln <- manyTill anyChar newline
spaces
return ln
-- parse digits
digit' :: Parser DAST.Nat
digit' = do
d <- many1 digit
spaces
return $ natify d
-- display error
err :: P.Stream s m t => String -> String -> P.ParsecT s u m a
err a b = unexpected ("--> " ++ a ++ " not valid: \"" ++ b ++ "\"")
notFoundError :: P.Stream s m t => String -> String -> P.ParsecT s u m a
notFoundError x a = unexpected ("--> the \"" ++ x ++ "\": \"" ++ a ++ "\" does not exist.")
antagNotFoundError :: P.Stream s m t => String -> P.ParsecT s u m a
antagNotFoundError a = notFoundError "antag" a
bulletNotFoundError :: P.Stream s m t => String -> P.ParsecT s u m a
bulletNotFoundError a = notFoundError "bullet" a
singleNotFoundError :: P.Stream s m t => String -> P.ParsecT s u m a
singleNotFoundError a = notFoundError "single" a
whatInvalidError :: P.Stream s m t => String -> P.ParsecT s u m a
whatInvalidError a = unexpected ("--> " ++ a ++ " is not a valid \'what\'")
ipInvalidError :: P.Stream s m t => String -> String -> P.ParsecT s u m a
ipInvalidError r c = unexpected ("--> initial position error, row: " ++ r ++ ", column: " ++ c)
-- indent
indented :: Show tok => SourcePos -> P.ParsecT [tok] u Data.Functor.Identity.Identity [a]
indented p = (eof >> return []) <|> do
innerPos <- getPosition
if sameIndent p innerPos then pzero else return []
-- options
options :: [(String,a)] -> Parser a
options l = choice $ map (\(s,r) -> do { _ <- reserved s; return r }) l
-- check if columns are inline
inline :: SourcePos -> P.ParsecT [tok] u Identity [a]
inline p = do
a <- getPosition
if sameIndent a p then return [] else pzero
-- check for same indent
sameIndent :: SourcePos -> SourcePos -> Bool
sameIndent a b = (sourceColumn a) == (sourceColumn b)
-- check for greater indent
greaterIndent :: SourcePos -> SourcePos -> Bool
greaterIndent a b = (sourceColumn a) > (sourceColumn b)