@@ -62,6 +62,7 @@ import System.FilePath
6262import System.IO
6363
6464{-
65+@
6566 grammar shortcut notation:
6667 ~CHARSEQ = complement of char sequence (i.e. any character except CHARSEQ)
6768 RULE? = optional rule (i.e. RULE or nothing)
@@ -69,12 +70,16 @@ import System.IO
6970 RULE+ = repeated rule with at least one match (i.e. RULE one or more times)
7071 RULE! = invalid rule (i.e. rule that is not valid in context, report meaningful error in case)
7172 RULE{n} = rule repeated n times
73+@
7274-}
7375
7476{- * Main grammar -}
7577
7678{- | Parses module definition
79+
80+@
7781 ModuleHeader ::= 'module' Identifier_t ';'?;
82+@
7883-}
7984moduleHeader :: IdrisParser [String]
8085moduleHeader = try (do reserved "module"
@@ -89,7 +94,10 @@ moduleHeader = try (do reserved "module"
8994 (x, '.':y) -> x : moduleName y
9095
9196{- | Parses an import statement
97+
98+@
9299 Import ::= 'import' Identifier_t ';'?;
100+@
93101 -}
94102import_ :: IdrisParser String
95103import_ = do reserved "import"
@@ -100,7 +108,10 @@ import_ = do reserved "import"
100108 where toPath f = foldl1' (</>) (Spl.splitOn "." f)
101109
102110{- | Parses program source
111+
112+@
103113 Prog ::= Decl* EOF;
114+@
104115 -}
105116prog :: SyntaxInfo -> IdrisParser [PDecl]
106117prog syn = do whiteSpace
@@ -110,7 +121,9 @@ prog syn = do whiteSpace
110121 let c = (concat decls)
111122 return c
112123
113−{- | Parses a top-level declaration
124+{-| Parses a top-level declaration
125+
126+@
114127Decl ::=
115128 Decl'
116129 | Using
@@ -125,6 +138,7 @@ Decl ::=
125138 | Transform
126139 | Import!
127140 ;
141+@
128142-}
129143decl :: SyntaxInfo -> IdrisParser [PDecl]
130144decl syn = do notEndBlock
@@ -150,6 +164,8 @@ decl syn = do notEndBlock
150164 return [d']
151165
152166{- | Parses a top-level declaration with possible syntax sugar
167+
168+@
153169Decl' ::=
154170 Fixity
155171 | FunDecl'
@@ -157,6 +173,7 @@ Decl' ::=
157173 | Record
158174 | SyntaxDecl
159175 ;
176+@
160177-}
161178decl' :: SyntaxInfo -> IdrisParser PDecl
162179decl' syn = fixity
@@ -167,7 +184,10 @@ decl' syn = fixity
167184 <?> "declaration"
168185
169186{- | Parses a syntax extension declaration (and adds the rule to parser state)
187+
188+@
170189 SyntaxDecl ::= SyntaxRule;
190+@
171191-}
172192syntaxDecl :: SyntaxInfo -> IdrisParser PDecl
173193syntaxDecl syn = do s <- syntaxRule syn
@@ -185,17 +205,24 @@ syntaxDecl syn = do s <- syntaxRule syn
185205 ename (Keyword n) = Just n
186206 ename _ = Nothing
187207
188−{- | Parses a syntax extension declaration
189−SyntaxRuleOpts ::= 'term' | 'pattern';
208+{- | Parses a syntax extension declaration
209+
210+@
211+SyntaxRuleOpts ::= 'term' | 'pattern';
212+@
190213
214+@
191215SyntaxRule ::=
192216 SyntaxRuleOpts? 'syntax' SyntaxSym+ '=' TypeExpr Terminator;
217+@
193218
219+@
194220SyntaxSym ::= '[' Name_t ']'
195221 | '{' Name_t '}'
196222 | Name_t
197223 | StringLiteral_t
198224 ;
225+@
199226-}
200227syntaxRule :: SyntaxInfo -> IdrisParser Syntax
201228syntaxRule syn
@@ -231,11 +258,14 @@ syntaxRule syn
231258
232259
233260{- | Parses a syntax symbol (either binding variable, keyword or expression)
261+
262+@
234263SyntaxSym ::= '[' Name_t ']'
235264 | '{' Name_t '}'
236265 | Name_t
237266 | StringLiteral_t
238267 ;
268+@
239269 -}
240270syntaxSym :: IdrisParser SSymbol
241271syntaxSym = try (do lchar '['; n <- name; lchar ']'
@@ -249,7 +279,10 @@ syntaxSym = try (do lchar '['; n <- name; lchar ']'
249279 <?> "syntax symbol"
250280
251281{- | Parses a function declaration with possible syntax sugar
282+
283+@
252284 FunDecl ::= FunDecl';
285+@
253286-}
254287fnDecl :: SyntaxInfo -> IdrisParser [PDecl]
255288fnDecl syn = try (do notEndBlock
@@ -258,13 +291,16 @@ fnDecl syn = try (do notEndBlock
258291 let d' = fmap (desugar syn i) d
259292 return [d']) <?> "function declaration"
260293
261−{- Parses a function declaration
294+{-| Parses a function declaration
295+
296+@
262297 FunDecl' ::=
263298 DocComment_t? FnOpts* Accessibility? FnOpts* FnName TypeSig Terminator
264299 | Postulate
265300 | Pattern
266301 | CAF
267302 ;
303+@
268304-}
269305fnDecl' :: SyntaxInfo -> IdrisParser PDecl
270306fnDecl' syn = checkFixity $
@@ -308,23 +344,29 @@ fnDecl' syn = checkFixity $
308344 | otherwise = return True
309345 fixityOK _ = return True
310346
311−{- Parses function options given initial options
347+{-| Parses function options given initial options
348+
349+@
312350FnOpts ::= 'total'
313351 | 'partial'
314352 | 'implicit'
315353 | '%' 'assert_total'
316354 | '%' 'error_handler'
317− | '%' 'reflection'
355+ | '%' 'reflection'
318356 | '%' 'specialise' '[' NameTimesList? ']'
319357 ;
358+@
320359
360+@
321361NameTimes ::= FnName Natural?;
362+@
322363
364+@
323365NameTimesList ::=
324366 NameTimes
325367 | NameTimes ',' NameTimesList
326368 ;
327−
369+@
328370-}
329371-- FIXME: Check compatability for function options (i.e. partal/total)
330372fnOpts :: [FnOpt] -> IdrisParser [FnOpt]
@@ -353,9 +395,11 @@ fnOpts opts
353395
354396{- | Parses a postulate
355397
398+@
356399Postulate ::=
357400 DocComment_t? 'postulate' FnOpts* Accesibility? FnOpts* FnName TypeSig Terminator
358401 ;
402+@
359403-}
360404postulate :: SyntaxInfo -> IdrisParser PDecl
361405postulate syn = do doc <- try $ do doc <- option "" (docComment '|')
@@ -381,9 +425,11 @@ postulate syn = do doc <- try $ do doc <- option "" (docComment '|')
381425
382426{- | Parses a using declaration
383427
428+@
384429Using ::=
385430 'using' '(' UsingDeclList ')' OpenBlock Decl* CloseBlock
386431 ;
432+@
387433 -}
388434using_ :: SyntaxInfo -> IdrisParser [PDecl]
389435using_ syn =
@@ -395,11 +441,13 @@ using_ syn =
395441 return (concat ds)
396442 <?> "using declaration"
397443
398−{- | Parses a parameters declaration
444+{- | Parses a parameters declaration
399445
446+@
400447Params ::=
401448 'parameters' '(' TypeDeclList ')' OpenBlock Decl* CloseBlock
402449 ;
450+@
403451 -}
404452params :: SyntaxInfo -> IdrisParser [PDecl]
405453params syn =
@@ -414,9 +462,11 @@ params syn =
414462
415463{- | Parses a mutual declaration (for mutually recursive functions)
416464
465+@
417466Mutual ::=
418467 'mutual' OpenBlock Decl* CloseBlock
419468 ;
469+@
420470-}
421471mutual :: SyntaxInfo -> IdrisParser [PDecl]
422472mutual syn =
@@ -429,11 +479,13 @@ mutual syn =
429479 return [PMutual fc (concat ds)]
430480 <?> "mutual block"
431481
432−{- | Parses a namespace declaration
482+{-| Parses a namespace declaration
433483
484+@
434485Namespace ::=
435486 'namespace' identifier OpenBlock Decl+ CloseBlock
436487 ;
488+@
437489-}
438490namespace :: SyntaxInfo -> IdrisParser [PDecl]
439491namespace syn =
@@ -444,9 +496,12 @@ namespace syn =
444496 return [PNamespace n (concat ds)]
445497 <?> "namespace declaration"
446498
447−{- |Parses a methods block (for instances)
499+{- | Parses a methods block (for instances)
500+
501+@
448502 InstanceBlock ::= 'where' OpenBlock FnDecl* CloseBlock
449− -}
503+@
504+-}
450505instanceBlock :: SyntaxInfo -> IdrisParser [PDecl]
451506instanceBlock syn = do reserved "where"
452507 openBlock
@@ -455,16 +510,20 @@ instanceBlock syn = do reserved "where"
455510 return (concat ds)
456511 <?> "instance block"
457512
458−{- |Parses a methods and instances block (for type classes)
513+{- | Parses a methods and instances block (for type classes)
459514
515+@
460516MethodOrInstance ::=
461517 FnDecl
462518 | Instance
463519 ;
520+@
464521
522+@
465523ClassBlock ::=
466524 'where' OpenBlock MethodOrInstance* CloseBlock
467525 ;
526+@
468527-}
469528classBlock :: SyntaxInfo -> IdrisParser [PDecl]
470529classBlock syn = do reserved "where"
@@ -474,16 +533,20 @@ classBlock syn = do reserved "where"
474533 return (concat ds)
475534 <?> "class block"
476535
477−{- |Parses a type class declaration
536+{-| Parses a type class declaration
478537
538+@
479539ClassArgument ::=
480540 Name
481541 | '(' Name ':' Expr ')'
482542 ;
543+@
483544
545+@
484546Class ::=
485547 DocComment_t? Accessibility? 'class' ConstraintList? Name ClassArgument* ClassBlock?
486548 ;
549+@
487550-}
488551class_ :: SyntaxInfo -> IdrisParser [PDecl]
489552class_ syn = do (doc, acc) <- try (do
@@ -504,13 +567,17 @@ class_ syn = do (doc, acc) <- try (do
504567 <|> do i <- name;
505568 return (i, PType)
506569
507−{- |Parses a type class instance declaration
570+{- | Parses a type class instance declaration
508571
572+@
509573 Instance ::=
510574 'instance' InstanceName? ConstraintList? Name SimpleExpr* InstanceBlock?
511575 ;
576+@
512577
513− InstanceName ::= '[' Name ']';
578+@
579+InstanceName ::= '[' Name ']';
580+@
514581-}
515582instance_ :: SyntaxInfo -> IdrisParser [PDecl]
516583instance_ syn = do reserved "instance"; fc <- getFC
@@ -531,20 +598,27 @@ instance_ syn = do reserved "instance"; fc <- getFC
531598
532599
533600{- | Parses a using declaration list
601+
602+@
534603UsingDeclList ::=
535604 UsingDeclList'
536605 | NameList TypeSig
537606 ;
607+@
538608
609+@
539610UsingDeclList' ::=
540611 UsingDecl
541612 | UsingDecl ',' UsingDeclList'
542613 ;
614+@
543615
616+@
544617NameList ::=
545618 Name
546619 | Name ',' NameList
547620 ;
621+@
548622-}
549623usingDeclList :: SyntaxInfo -> IdrisParser [Using]
550624usingDeclList syn
@@ -556,10 +630,13 @@ usingDeclList syn
556630 <?> "using declaration list"
557631
558632{- |Parses a using declaration
633+
634+@
559635UsingDecl ::=
560636 FnName TypeSig
561637 | FnName FnName+
562638 ;
639+@
563640-}
564641usingDecl :: SyntaxInfo -> IdrisParser Using
565642usingDecl syn = try (do x <- fnName
@@ -571,8 +648,11 @@ usingDecl syn = try (do x <- fnName
571648 return (UConstraint c xs)
572649 <?> "using declaration"
573650
574−{- | Parse a clause with patterns
651+{- | Parse a clause with patterns
652+
653+@
575654Pattern ::= Clause;
655+@
576656-}
577657pattern :: SyntaxInfo -> IdrisParser PDecl
578658pattern syn = do fc <- getFC
@@ -581,7 +661,10 @@ pattern syn = do fc <- getFC
581661 <?> "pattern"
582662
583663{- | Parse a constant applicative form declaration
584− CAF ::= 'let' FnName '=' Expr Terminator;
664+
665+@
666+CAF ::= 'let' FnName '=' Expr Terminator;
667+@
585668-}
586669caf :: SyntaxInfo -> IdrisParser PDecl
587670caf syn = do reserved "let"
@@ -594,7 +677,10 @@ caf syn = do reserved "let"
594677 <?> "constant applicative form declaration"
595678
596679{- | Parse an argument expression
597− ArgExpr ::= HSimpleExpr | {- In Pattern External (User-defined) Expression -};
680+
681+@
682+ArgExpr ::= HSimpleExpr | {- In Pattern External (User-defined) Expression -};
683+@
598684-}
599685argExpr :: SyntaxInfo -> IdrisParser PTerm
600686argExpr syn = let syn' = syn { inPattern = True } in
@@ -602,12 +688,17 @@ argExpr syn = let syn' = syn { inPattern = True } in
602688 <?> "argument expression"
603689
604690{- | Parse a right hand side of a function
691+
692+@
605693RHS ::= '=' Expr
606694 | '?=' RHSName? Expr
607695 | 'impossible'
608696 ;
697+@
609698
699+@
610700RHSName ::= '{' FnName '}';
701+@
611702-}
612703rhs :: SyntaxInfo -> Name -> IdrisParser PTerm
613704rhs syn n = do lchar '='; expr syn
@@ -630,16 +721,28 @@ rhs syn n = do lchar '='; expr syn
630721 addLet nm r = (PLet (sUN "value") Placeholder r (PMetavar nm))
631722
632723{- |Parses a function clause
724+
725+@
633726RHSOrWithBlock ::= RHS WhereOrTerminator
634727 | 'with' SimpleExpr OpenBlock FnDecl+ CloseBlock
635728 ;
729+@
730+
731+@
636732Clause ::= WExpr+ RHSOrWithBlock
637733 | SimpleExpr '<==' FnName RHS WhereOrTerminator
638734 | ArgExpr Operator ArgExpr WExpr* RHSOrWithBlock {- Except "=" and "?=" operators to avoid ambiguity -}
639735 | FnName ConstraintArg* ImplicitOrArgExpr* WExpr* RHSOrWithBlock
640736 ;
737+@
738+
739+@
641740ImplicitOrArgExpr ::= ImplicitArg | ArgExpr;
642−WhereOrTerminator ::= WhereBlock | Terminator;
741+@
742+
743+@
744+WhereOrTerminator ::= WhereBlock | Terminator;
745+@
643746-}
644747clause :: SyntaxInfo -> IdrisParser PClause
645748clause syn
@@ -768,17 +871,23 @@ clause syn
768871 fillLHSD n c a (PClauses fc o fn cs) = PClauses fc o fn (map (fillLHS n c a) cs)
769872 fillLHSD n c a x = x
770873
771−{- |Parses with pattern
772− WExpr ::= '|' Expr';
874+{-| Parses with pattern
875+
876+@
877+WExpr ::= '|' Expr';
878+@
773879-}
774880wExpr :: SyntaxInfo -> IdrisParser PTerm
775881wExpr syn = do lchar '|'
776882 expr' syn
777883 <?> "with pattern"
778884
779−{- |Parses a where block
885+{- | Parses a where block
886+
887+@
780888WhereBlock ::= 'where' OpenBlock Decl+ CloseBlock;
781− -}
889+@
890+-}
782891whereBlock :: Name -> SyntaxInfo -> IdrisParser ([PDecl], [(Name, Name)])
783892whereBlock n syn
784893 = do reserved "where"
@@ -788,6 +897,8 @@ whereBlock n syn
788897 <?> "where block"
789898
790899{- |Parses a code generation target language name
900+
901+@
791902Codegen ::= 'C'
792903 | 'Java'
793904 | 'JavaScript'
@@ -795,6 +906,7 @@ Codegen ::= 'C'
795906 | 'LLVM'
796907 | 'Bytecode'
797908 ;
909+@
798910-}
799911codegen_ :: IdrisParser Codegen
800912codegen_ = do reserved "C"; return ViaC
@@ -806,13 +918,18 @@ codegen_ = do reserved "C"; return ViaC
806918 <?> "code generation language"
807919
808920{- |Parses a compiler directive
921+@
809922StringList ::=
810923 String
811924 | String ',' StringList
812925 ;
926+@
813927
928+@
814929Directive ::= '%' Directive';
930+@
815931
932+@
816933Directive' ::= 'lib' CodeGen String_t
817934 | 'link' CodeGen String_t
818935 | 'flag' CodeGen String_t
@@ -827,6 +944,7 @@ Directive' ::= 'lib' CodeGen String_t
827944 | 'language' 'TypeProviders'
828945 | 'language' 'ErrorReflection'
829946 ;
947+@
830948-}
831949directive :: SyntaxInfo -> IdrisParser [PDecl]
832950directive syn = do try (lchar '%' *> reserved "lib"); cgn <- codegen_; lib <- stringLiteral;
@@ -886,15 +1004,22 @@ pLangExt = (reserved "TypeProviders" >> return TypeProviders)
8861004 <|> (reserved "ErrorReflection" >> return ErrorReflection)
8871005
8881006{- | Parses a totality
1007+
1008+@
8891009Totality ::= 'partial' | 'total'
1010+@
1011+
8901012-}
8911013totality :: IdrisParser Bool
8921014totality
8931015 = do reserved "total"; return True
8941016 <|> do reserved "partial"; return False
8951017
896−{- | Parses a type provider
1018+{- | Parses a type provider
1019+
1020+@
8971021Provider ::= '%' 'provide' '(' FnName TypeSig ')' 'with' Expr;
1022+@
8981023 -}
8991024provider :: SyntaxInfo -> IdrisParser [PDecl]
9001025provider syn = do try (lchar '%' *> reserved "provide");
@@ -906,7 +1031,10 @@ provider syn = do try (lchar '%' *> reserved "provide");
9061031 <?> "type provider"
9071032
9081033{- | Parses a transform
1034+
1035+@
9091036Transform ::= '%' 'transform' Expr '==>' Expr
1037+@
9101038-}
9111039transform :: SyntaxInfo -> IdrisParser [PDecl]
9121040transform syn = do try (lchar '%' *> reserved "transform")