-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathSyntaxTests.hs
More file actions
240 lines (203 loc) · 6.42 KB
/
SyntaxTests.hs
File metadata and controls
240 lines (203 loc) · 6.42 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.Futhark.SyntaxTests (tests) where
import Control.Applicative hiding (many, some)
import Data.Bifunctor
import Data.Char (isAlpha)
import Data.Functor
import Data.Map qualified as M
import Data.String
import Data.Text qualified as T
import Data.Void
import Language.Futhark
import Language.Futhark.Parser (SyntaxError (syntaxErrorMsg), parseExp, parseType)
import Language.Futhark.Primitive.Parse (constituent, keyword, lexeme)
import Language.Futhark.PrimitiveTests ()
import Test.QuickCheck
import Test.Tasty
import Text.Megaparsec
import Text.Megaparsec.Char.Lexer qualified as L
import Prelude
tests :: TestTree
tests = testGroup "Source SyntaxTests" []
instance Arbitrary BinOp where
arbitrary = elements [minBound .. maxBound]
instance Arbitrary Uniqueness where
arbitrary = elements [Unique, Nonunique]
instance Arbitrary PrimType where
arbitrary =
oneof
[ Signed <$> arbitrary,
Unsigned <$> arbitrary,
FloatType <$> arbitrary,
pure Bool
]
instance Arbitrary PrimValue where
arbitrary =
oneof
[ SignedValue <$> arbitrary,
UnsignedValue <$> arbitrary,
FloatValue <$> arbitrary,
BoolValue <$> arbitrary
]
-- The following dirty instances make it slightly nicer to write unit tests.
instance IsString VName where
fromString s =
let (tag, s') = bimap reverse (reverse . tail) $ span (/= '_') $ reverse s
in VName (fromString s') (read tag)
instance (IsString v) => IsString (QualName v) where
fromString = QualName [] . fromString
instance IsString UncheckedTypeExp where
fromString =
either (error . T.unpack . syntaxErrorMsg) id
. parseType "IsString UncheckedTypeExp"
. fromString
type Parser = Parsec Void T.Text
braces, brackets, parens :: Parser a -> Parser a
braces = between (lexeme "{") (lexeme "}")
brackets = between (lexeme "[") (lexeme "]")
parens = between (lexeme "(") (lexeme ")")
pName :: Parser Name
pName =
lexeme . fmap nameFromString $
(:) <$> satisfy isAlpha <*> many (satisfy constituent)
pVName :: Parser VName
pVName = lexeme $ do
(s, tag) <-
satisfy constituent
`manyTill_` try pTag
<?> "variable name"
pure $ VName (nameFromString s) tag
where
pTag =
"_" *> L.decimal <* notFollowedBy (satisfy constituent)
pQualName :: Parser (QualName VName)
pQualName = QualName [] <$> pVName
pPrimType :: Parser PrimType
pPrimType =
choice $
map
f
[ Bool,
Signed Int8,
Signed Int16,
Signed Int32,
Signed Int64,
Unsigned Int8,
Unsigned Int16,
Unsigned Int32,
Unsigned Int64,
FloatType Float32,
FloatType Float64
]
where
f t = keyword (prettyText t) $> t
pUniqueness :: Parser Uniqueness
pUniqueness = choice [lexeme "*" $> Unique, pure Nonunique]
pDim :: Parser d -> Parser d
pDim = brackets
pSize :: Parser Size
pSize =
choice
[ flip sizeFromInteger mempty <$> lexeme L.decimal,
flip sizeFromName mempty <$> pQualName
]
pScalarNonFun :: Parser d -> Parser (ScalarTypeBase d Uniqueness)
pScalarNonFun pd =
choice
[ Prim <$> pPrimType,
pTypeVar,
tupleRecord <$> parens (pType pd `sepBy` lexeme ","),
Record . M.fromList <$> braces (pField `sepBy1` lexeme ",")
]
where
pField = (,) <$> pName <* lexeme ":" <*> pType pd
pTypeVar = TypeVar <$> pUniqueness <*> pQualName <*> many pTypeArg
pTypeArg =
choice
[ TypeArgDim <$> pDim pd,
TypeArgType . second (const NoUniqueness) <$> pTypeArgType
]
pTypeArgType =
choice
[ Scalar . Prim <$> pPrimType,
parens $ pType pd
]
pArrayType :: Parser d -> Parser (TypeBase d Uniqueness)
pArrayType pd =
Array
<$> pUniqueness
<*> (Shape <$> some (pDim pd))
<*> (second (const NoUniqueness) <$> pScalarNonFun pd)
pNonFunType :: Parser d -> Parser (TypeBase d Uniqueness)
pNonFunType pd =
choice
[ try $ pArrayType pd,
try $ parens $ pType pd,
Scalar <$> pScalarNonFun pd
]
uniquenessToDiet :: Uniqueness -> Diet
uniquenessToDiet Unique = Consume
uniquenessToDiet Nonunique = Observe
pScalarType :: Parser d -> Parser (ScalarTypeBase d Uniqueness)
pScalarType pd = choice [try pFun, pScalarNonFun pd]
where
pFun =
pParam <* lexeme "->" <*> pRetType pd
pParam =
choice
[ try pNamedParam,
do
t <- pNonFunType pd
pure $ Arrow Nonunique Unnamed (diet $ second uniquenessToDiet t) (toStruct t)
]
pNamedParam = parens $ do
v <- pVName <* lexeme ":"
t <- pType pd
pure $ Arrow Nonunique (Named v) (diet $ second uniquenessToDiet t) (toStruct t)
pRetType :: Parser d -> Parser (RetTypeBase d Uniqueness)
pRetType pd =
choice
[ lexeme "?" *> (RetType <$> some (brackets pVName) <* lexeme "." <*> pType pd),
RetType [] <$> pType pd
]
pType :: Parser d -> Parser (TypeBase d Uniqueness)
pType pd =
choice [try $ Scalar <$> pScalarType pd, pArrayType pd, parens (pType pd)]
fromStringParse :: Parser a -> String -> String -> a
fromStringParse p what s =
either onError id $ parse (p <* eof) "" (T.pack s)
where
onError e =
error $ "not a " <> what <> ": " <> s <> "\n" <> errorBundlePretty e
instance IsString (ScalarTypeBase Size NoUniqueness) where
fromString =
fromStringParse
(second (const NoUniqueness) <$> pScalarType pSize)
"ScalarType"
instance IsString (ScalarTypeBase () NoUniqueness) where
fromString =
fromStringParse
(second (const NoUniqueness) <$> pScalarType (pure ()))
"ScalarType"
instance IsString (TypeBase () NoUniqueness) where
fromString =
fromStringParse (second (const NoUniqueness) <$> pType (pure ())) "Type"
instance IsString StructType where
fromString =
fromStringParse (second (const NoUniqueness) <$> pType pSize) "StructType"
instance IsString ParamType where
fromString =
fromStringParse (resToParam <$> pType pSize) "ParamType"
instance IsString ResType where
fromString =
fromStringParse (pType pSize) "ResType"
instance IsString StructRetType where
fromString =
fromStringParse (second (pure NoUniqueness) <$> pRetType pSize) "StructRetType"
instance IsString ResRetType where
fromString = fromStringParse (pRetType pSize) "ResRetType"
instance IsString UncheckedExp where
fromString =
either (error . T.unpack . syntaxErrorMsg) id
. parseExp "string literal"
. T.pack