-
Notifications
You must be signed in to change notification settings - Fork 7
/
Parser.hs
741 lines (631 loc) · 20.5 KB
/
Parser.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
{-# language DuplicateRecordFields #-}
{-# language OverloadedStrings #-}
{-# language RankNTypes #-}
module Parser where
import Protolude hiding (break, try, moduleName)
import Control.Monad.Fail
import qualified Data.HashMap.Lazy as HashMap
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import Text.Parser.Combinators (sepBy, sepBy1)
import Boxity
import qualified Error.Parsing as Error
import qualified Lexer
import Lexer (Token(Token), UnspannedToken)
import qualified Literal
import qualified Module
import Name (Name(Name))
import qualified Name
import Plicity
import qualified Position
import Presyntax hiding (clause)
import qualified Presyntax
import qualified Span
parseTokens :: Parser a -> [Token] -> Either Error.Parsing a
parseTokens p tokens =
unParser
p
(\a _ -> Right a)
(\a _ _ -> Right a)
(\err -> mkError err tokens)
(\err tokens' -> mkError err tokens')
tokens
(Position.LineColumn 0 0)
(Position.Absolute 0)
where
mkError err tokens' =
Left Error.Parsing
{ Error.reason = _reason err
, Error.expected = toList $ _expected err
, Error.position =
case tokens' of
[] ->
Left Error.EOF
Token _ (Span.Absolute pos _) _:_ ->
Right pos
}
-------------------------------------------------------------------------------
data ErrorReason = ErrorReason
{ _reason :: Maybe Text
, _expected :: HashSet Text
} deriving Show
instance Semigroup ErrorReason where
ErrorReason r1 e1 <> ErrorReason r2 e2 =
ErrorReason (r1 <|> r2) (e1 <> e2)
instance Monoid ErrorReason where
mempty =
ErrorReason empty mempty
failed :: Text -> ErrorReason
failed reason =
ErrorReason (Just reason) mempty
expected :: Text -> ErrorReason
expected s =
ErrorReason Nothing $ HashSet.singleton s
-------------------------------------------------------------------------------
data Parser a = Parser
{ unParser
:: forall r
. (a -> ErrorReason -> r) -- success epsilon
-> (a -> ErrorReason -> [Token] -> r) -- success committed
-> (ErrorReason -> r) -- error epsilon
-> (ErrorReason -> [Token] -> r) -- error committed
-> [Token] -- input
-> Position.LineColumn -- indentation base
-> Position.Absolute -- base position
-> r
}
instance Semigroup a => Semigroup (Parser a) where
(<>) = liftA2 (<>)
instance Monoid a => Monoid (Parser a) where
mempty = pure mempty
instance Functor Parser where
fmap f (Parser p) =
Parser $ \s0 s -> p (s0 . f) (s . f)
instance Applicative Parser where
pure a = Parser $ \s0 _s _e0 _e _inp _lineCol _base -> s0 a mempty
(<*>) = ap
instance Alternative Parser where
empty = Parser $ \_s0 _s e0 _e _inp _lineCol _base -> e0 mempty
Parser p <|> Parser q =
Parser $ \s0 s e0 e inp lineCol base ->
p
s0
s
(\err -> q s0 s (\err' -> e0 $ err <> err') e inp lineCol base)
e
inp
lineCol
base
many p = reverse <$> manyAccum (:) p
some p = (:) <$> p <*> many p
instance Monad Parser where
return = pure
Parser p >>= f =
Parser $ \s0 s e0 e inp lineCol base ->
p
(\a err -> unParser (f a)
(\b err' -> s0 b $ err <> err')
s
(\err' -> e0 $ err <> err')
e
inp
lineCol
base)
(\a err inp' -> unParser (f a)
(\b err' -> s b (err <> err') inp')
s
(\err' -> e (err <> err') inp')
e
inp'
lineCol
base)
e0
e
inp
lineCol
base
instance MonadFail Parser where
fail x =
Parser $ \_s0 _s e0 _e _inp _lineCol _base -> e0 $ failed $ toS x
instance MonadPlus Parser where
mzero = empty
mplus = (<|>)
eof :: Parser ()
eof =
Parser $ \s0 _ e0 _ inp _ _ ->
case inp of
[] ->
s0 () mempty
_:_ ->
e0 $ expected "EOF"
manyAccum :: (a -> [a] -> [a]) -> Parser a -> Parser [a]
manyAccum f (Parser p) =
Parser $ \s0 s _e0 e inp lineCol base -> do
let manyFailed inp' _ _ =
e (failed "'many' applied to a parser that accepts an empty string") inp'
walk xs x err inp' =
p
(manyFailed inp')
(walk $ f x xs)
(\err' -> s (f x xs) (err <> err') inp')
e
inp'
lineCol
base
p (manyFailed inp) (walk []) (s0 []) e inp lineCol base
try :: Parser a -> Parser a
try (Parser p) =
Parser $ \s0 s e0 _e -> p s0 s e0 (\_err _inp -> e0 mempty)
(<?>) :: Parser a -> Text -> Parser a
Parser p <?> expect =
Parser $ \s0 s e0 e ->
p
(\a -> s0 a . setExpected)
s
(e0 . setExpected)
e
where
setExpected e = e { _expected = HashSet.singleton expect }
notFollowedBy :: Parser Text -> Parser ()
notFollowedBy (Parser p) =
Parser $ \s0 _ e0 _ ->
p
(\a _ -> e0 $ failed $ "Unexpected '" <> a <> "'")
(\a _ _ -> e0 $ failed $ "Unexpected '" <> a <> "'")
(\_ -> s0 () mempty)
(\_ _ -> s0 () mempty)
notFollowedByToken :: UnspannedToken -> Parser ()
notFollowedByToken token_ =
notFollowedBy $ Lexer.displayToken token_ <$ token token_
withRecovery :: (ErrorReason -> Position.Absolute -> [Token] -> Parser a) -> Parser a -> Parser a
withRecovery recover_ (Parser p) = Parser
$ \s0 s e0 e inp lineCol base ->
p
s0
s
(\err -> unParser (recover_ err base inp)
(\a _err' -> s0 a err)
s
(\_err' -> e0 err)
(\_err' _inp' -> e0 err)
inp
lineCol
base)
(\err inp' -> unParser (recover_ err base inp')
(\a _err' -> s a err inp)
s
(\_err' -> e err inp')
(\_err' _inp'' -> e err inp')
inp
lineCol
base)
inp
lineCol
base
withToken
::
(forall r
. (a -> r)
-> (ErrorReason -> r)
-> Span.Relative
-> UnspannedToken
-> r
)
-> Parser a
withToken f =
Parser $ \_s0 s e0 _e inp _lineCol base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token _pos tokenSpan token_:inp' ->
f
(\a -> s a mempty inp')
e0
(Span.relativeTo base tokenSpan)
token_
withIndentedToken
::
(forall r
. (a -> r)
-> (ErrorReason -> r)
-> Span.Relative
-> UnspannedToken
-> r
)
-> Parser a
withIndentedToken f =
Parser $ \_s0 s e0 _e inp (Position.LineColumn line col) base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token (Position.LineColumn tokenLine tokenCol) tokenSpan token_:inp'
| line == tokenLine || col < tokenCol ->
f
(\a -> s a mempty inp')
e0
(Span.relativeTo base tokenSpan)
token_
| otherwise ->
e0 $ failed "Unexpected unindent"
withIndentedTokenM
::
(forall r
. (Parser a -> r)
-> r
-> Span.Relative
-> UnspannedToken
-> r
)
-> Parser a
withIndentedTokenM f =
Parser $ \_s0 s e0 e inp lineCol@(Position.LineColumn line col) base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token (Position.LineColumn tokenLine tokenCol) tokenSpan token_:inp'
| line == tokenLine || col < tokenCol ->
f
(\p ->
unParser
p
(\a err -> s a err inp')
s
(\err -> e err inp')
e
inp'
lineCol
base
)
(e0 mempty)
(Span.relativeTo base tokenSpan)
token_
| otherwise ->
e0 $ failed "Unexpected unindent"
withIndentationBlock :: Parser a -> Parser a
withIndentationBlock p =
Parser $ \s0 s e0 e inp _lineCol base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token tokenLineCol _ _:_ ->
unParser p s0 s e0 e inp tokenLineCol base
relativeTo :: Parser a -> Parser (Position.Absolute, a)
relativeTo p =
Parser $ \s0 s e0 e inp lineCol _base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token _ (Span.Absolute tokenStart _) _:_ ->
unParser ((,) tokenStart <$> p) s0 s e0 e inp lineCol tokenStart
sameLevel :: Parser a -> Parser a
sameLevel p =
Parser $ \s0 s e0 e inp lineCol@(Position.LineColumn _ col) base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token (Position.LineColumn _ tokenCol) _ _:_
| col == tokenCol ->
unParser p s0 s e0 e inp lineCol base
| col > tokenCol ->
e0 $ failed "Unexpected unindent"
| otherwise ->
e0 $ failed "Unexpected indent"
indented :: Parser a -> Parser a
indented p =
Parser $ \s0 s e0 e inp lineCol@(Position.LineColumn line col) base ->
case inp of
[] ->
e0 $ failed "Unexpected EOF"
Token (Position.LineColumn tokenLine tokenCol) _ _:_
| line == tokenLine || col < tokenCol ->
unParser p s0 s e0 e inp lineCol base
| otherwise ->
e0 $ failed "Unexpected unindent"
-- | One or more at the same indentation level.
someSame :: Parser a -> Parser [a]
someSame p =
some (sameLevel $ withIndentationBlock p)
-- | Zero or more at the same indentation level.
manySame :: Parser a -> Parser [a]
manySame p =
many (sameLevel $ withIndentationBlock p)
blockOfMany :: Parser a -> Parser [a]
blockOfMany p =
indented (withIndentationBlock $ someSame p)
<|> pure []
token :: UnspannedToken -> Parser Span.Relative
token t =
withIndentedToken $ \continue break span t' ->
if t == t' then
continue span
else
break $ expected $ "'" <> Lexer.displayToken t <> "'"
uncheckedToken :: UnspannedToken -> Parser Span.Relative
uncheckedToken t =
withToken $ \continue break span t' ->
if t == t' then
continue span
else
break $ expected $ "'" <> Lexer.displayToken t <> "'"
spannedIdentifier :: Parser (Span.Relative, Text)
spannedIdentifier =
withIndentedToken $ \continue break span token_ ->
case token_ of
Lexer.Identifier name_ ->
continue (span, name_)
_ ->
break $ expected "identifier"
spannedPrename :: Parser (Span.Relative, Name.Pre)
spannedPrename =
second Name.Pre <$> spannedIdentifier
spannedModuleName :: Parser (Span.Relative, Name.Module)
spannedModuleName =
second Name.Module <$> spannedIdentifier
spannedName :: Parser (Span.Relative, Name)
spannedName =
second Name <$> spannedIdentifier
spannedConstructor :: Parser (Span.Relative, Name.Constructor)
spannedConstructor =
second Name.Constructor <$> spannedIdentifier
binding :: Parser Binding
binding =
uncurry Binding <$> spannedName
-------------------------------------------------------------------------------
-- Error recovery
recover :: (Error.Parsing -> a) -> ErrorReason -> Position.Absolute -> [Token] -> Parser a
recover k errorReason _base inp = do
skipToBaseLevel
pure $
k $
Error.Parsing
(_reason errorReason)
(HashSet.toList $ _expected errorReason)
pos
where
pos =
case inp of
Token _ (Span.Absolute startPos _) _:_ ->
Right startPos
_ ->
Left Error.EOF
skipToBaseLevel :: Parser ()
skipToBaseLevel =
Parser $ \_s0 s e0 _e inp lineCol _base ->
case inp of
_:inp' ->
s () mempty $ dropWhile (\(Token tokenLineCol _ _) -> lineCol < tokenLineCol) inp'
_ ->
e0 $ failed "Unexpected EOF"
-------------------------------------------------------------------------------
-- Patterns
atomicPattern :: Parser Pattern
atomicPattern =
(<?> "pattern") $
withIndentedTokenM $ \continue break span token_ ->
case token_ of
Lexer.LeftParen ->
continue $ pattern_ <* token Lexer.RightParen
Lexer.QuestionMark ->
continue $ pure $ Pattern span $ ConOrVar span "?" mempty
Lexer.Underscore ->
continue $ pure $ Pattern span $ WildcardPattern
Lexer.Forced ->
continue $ (\term_@(Term termSpan _) -> Pattern termSpan $ Forced term_) <$> atomicTerm
Lexer.Identifier name_ ->
continue $ pure $ Pattern span $ ConOrVar span (Name.Pre name_) mempty
Lexer.Number int ->
continue $ pure $ Pattern span $ LitPattern $ Literal.Integer int
_ ->
break
pattern_ :: Parser Pattern
pattern_ =
( uncurry conOrVar <$> spannedPrename <*> many plicitPattern
<|> atomicPattern
)
<**>
( flip anno <$ token Lexer.Colon <*> term
<|> pure identity
) <?> "pattern"
plicitPattern :: Parser PlicitPattern
plicitPattern =
mkImplicitPattern <$>
token Lexer.LeftImplicitBrace <*>
sepBy patName (token $ Lexer.Operator ",") <*>
token Lexer.RightImplicitBrace
<|> ExplicitPattern <$> atomicPattern
<?> "explicit or implicit pattern"
where
mkImplicitPattern span1 pats span2 =
ImplicitPattern (Span.add span1 span2) $ HashMap.fromList pats
patName =
spannedName <**>
((\pat (_, name_) -> (name_, pat)) <$ token Lexer.Equals <*> pattern_
<|> pure (\(span, name_@(Name n)) -> (name_, Pattern span $ ConOrVar span (Name.Pre n) mempty))
)
-------------------------------------------------------------------------------
-- Terms
recoveringTerm :: Parser Term
recoveringTerm =
withRecovery
(\errorInfo base inp' ->
case inp' of
Token _ tokenSpan _:_ ->
recover (Term (Span.relativeTo base tokenSpan) . ParseError) errorInfo base inp'
_ ->
empty
)
term
atomicTerm :: Parser Term
atomicTerm =
(<?> "term") $
withIndentedTokenM $ \continue break span token_ ->
case token_ of
Lexer.LeftParen ->
continue $ term <* token Lexer.RightParen
Lexer.Let ->
continue $
flip (foldr ($)) <$> blockOfMany letBinding <* token Lexer.In <*> term
Lexer.Underscore ->
continue $ pure $ Term span Wildcard
Lexer.QuestionMark ->
continue $ pure $ Term span Wildcard
Lexer.Identifier ident ->
continue $ pure $ Term span $ Var $ Name.Pre ident
Lexer.Case ->
continue $
case_ span <$> term <*> token Lexer.Of <*> blockOfMany branch
Lexer.Lambda ->
continue $
lams <$> some plicitPattern <* token Lexer.Dot <*> term
Lexer.Forall ->
continue $
implicitPis <$>
some
( (,) <$ token Lexer.LeftParen <*> some binding <* token Lexer.Colon <*> term <* token Lexer.RightParen
<|> (\binding_@(Binding span_ _) -> ([binding_], Term span_ Wildcard)) <$> binding
)
<* token Lexer.Dot <*> term
Lexer.Number int ->
continue $ pure $ Term span $ Lit $ Literal.Integer int
_ ->
break
where
implicitPis vss target =
foldr (\(vs, domain) target' -> pis Implicit vs domain target') target vss
branch :: Parser (Pattern, Term)
branch =
(,) <$> pattern_ <* token Lexer.RightArrow <*> term
letBinding :: Parser (Term -> Term)
letBinding = do
Binding span name_@(Name nameText) <- binding
let_ span name_ . Just . (,) span <$ token Lexer.Colon <*> recoveringTerm <*>
sameLevel (withIndentationBlock $ do
span' <- token $ Lexer.Identifier nameText
clauses span' nameText)
<|> let_ span name_ Nothing <$> clauses span nameText
plicitAtomicTerm :: Parser (Term -> Term)
plicitAtomicTerm =
(\args span fun -> implicitApp fun (HashMap.fromList args) span) <$ token Lexer.LeftImplicitBrace <*>
sepBy implicitArgument (token $ Lexer.Operator ",") <*>
token Lexer.RightImplicitBrace
<|> flip app <$> atomicTerm
where
implicitArgument =
spannedName <**>
((\t (_, n) -> (n, t)) <$ token Lexer.Equals <*> term
<|> pure (\(span, n@(Name text)) -> (n, Term span $ Var $ Name.Pre text))
)
term :: Parser Term
term =
pis Explicit <$>
try (token Lexer.LeftParen *> some binding <* token Lexer.Colon) <*>
term <* token Lexer.RightParen <*
token Lexer.RightArrow <*> term
<|> atomicTerm <**> (foldl' (flip (.)) identity <$> many plicitAtomicTerm) <**> fun
<?> "term"
where
fun =
flip function <$ token Lexer.RightArrow <*> term
<|> pure identity
-------------------------------------------------------------------------------
-- Definitions
definition :: Parser (Either Error.Parsing (Position.Absolute, (Name, Definition)))
definition =
withRecovery (recover Left) $
fmap Right $
sameLevel $
withIndentationBlock $
relativeTo $
dataDefinition
<|> do
(span, name_@(Name nameText)) <- spannedName
(,) name_ <$>
(TypeDeclaration span <$ token Lexer.Colon <*> recoveringTerm
<|> ConstantDefinition <$> clauses span nameText
)
<?> "definition"
clauses :: Span.Relative -> Text -> Parser [(Span.Relative, Clause)]
clauses firstSpan nameText =
(:) <$>
((,) firstSpan <$> clause) <*>
manySame ((,) <$> try (token (Lexer.Identifier nameText) <* notFollowedByToken Lexer.Colon) <*> clause)
clause :: Parser Clause
clause =
Presyntax.clause <$> many plicitPattern <*> token Lexer.Equals <*> recoveringTerm
dataDefinition :: Parser (Name, Definition)
dataDefinition =
mkDataDefinition <$> boxity <*> spannedName <*> parameters <*>
(token Lexer.Where *> blockOfMany gadtConstructors
<|> token Lexer.Equals *> sepBy1 adtConstructor (token Lexer.Pipe)
)
where
boxity =
Boxed <$ token (Lexer.Identifier "boxed") <* uncheckedToken Lexer.Data
<|> Unboxed <$ token Lexer.Data
mkDataDefinition boxity_ (span, name_) params constrs =
(name_, DataDefinition span boxity_ params constrs)
parameters =
parameters1 <|> pure []
parameters1 =
implicitParameters
<|> (<>) <$> explicitParameter <*> parameters
explicitParameter =
(\bindings type_ -> [(binding_, type_, Explicit) | binding_ <- bindings]) <$ token Lexer.LeftParen <*> some binding <* token Lexer.Colon <*> recoveringTerm <* token Lexer.RightParen
<|> (\binding_@(Binding span _) -> pure (binding_, Term span Presyntax.Wildcard, Explicit)) <$> binding
implicitParameters =
(<>) . concat <$ token Lexer.Forall <*>
some
((\bindings type_ -> [(binding_, type_, Implicit) | binding_ <- bindings]) <$ token Lexer.LeftParen <*> some binding <* token Lexer.Colon <*> term <* token Lexer.RightParen
<|> (\binding_@(Binding span _) -> [(binding_, Term span Wildcard, Implicit)]) <$> binding
) <* token Lexer.Dot <*> parameters1
gadtConstructors =
withIndentationBlock $
GADTConstructors <$> some spannedConstructor <* token Lexer.Colon <*> recoveringTerm
adtConstructor =
uncurry ADTConstructor <$> spannedConstructor <*> many atomicTerm
-------------------------------------------------------------------------------
-- Module
module_ :: Parser ((Maybe (Span.Absolute, Name.Module), Module.Header), [Either Error.Parsing (Position.Absolute, (Name, Definition))])
module_ =
(,) <$> moduleHeader <*> many definition
moduleHeader :: Parser (Maybe (Span.Absolute, Name.Module), Module.Header)
moduleHeader =
mkModuleHeader <$> moduleExposing <*> manySame import_
where
mkModuleHeader (mname, exposed) imports =
(mname, Module.Header exposed imports)
moduleExposing =
sameLevel $ withIndentationBlock ((\(span, name) exposed -> (Just (Span.absoluteFrom 0 span, name), exposed)) <$ token (Lexer.Identifier "module") <*> spannedModuleName <* token (Lexer.Identifier "exposing") <*> exposedNames)
<|> pure (Nothing, Module.AllExposed)
import_ :: Parser Module.Import
import_ =
sameLevel $
withIndentationBlock $
mkImport
<$ token (Lexer.Identifier "import") <*> spannedModuleName
<*>
(Just <$ token (Lexer.Identifier "as") <*> spannedPrename
<|> pure Nothing
)
<*>
(Just <$ token (Lexer.Identifier "exposing") <*> exposedNames
<|> pure Nothing
)
where
mkImport (span, n@(Name.Module text)) malias mexposed =
Module.Import
{ _span = absoluteSpan
, _module = n
, _alias = maybe (absoluteSpan, Name.Pre text) (first $ Span.absoluteFrom 0) malias
, _importedNames = fold mexposed
}
where
absoluteSpan =
Span.absoluteFrom 0 span
exposedNames :: Parser Module.ExposedNames
exposedNames =
token Lexer.LeftParen *> inner <* token Lexer.RightParen
where
inner =
Module.AllExposed <$ token (Lexer.Operator "..")
<|> Module.Exposed . HashSet.fromList <$> sepBy (snd <$> spannedPrename) (token $ Lexer.Operator ",")