e3f45f9475e1cf543b2ba7db9f9110ed4ef34c83
[ghc-hetmet.git] / compiler / parser / HaddockParse.y
1 {
2 {-# OPTIONS -w #-}
3 -- The above warning supression flag is a temporary kludge.
4 -- While working on this module you are encouraged to remove it and fix
5 -- any warnings in the module. See
6 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
7 -- for details
8
9 module HaddockParse (
10   parseHaddockParagraphs, 
11   parseHaddockString, 
12   MyEither(..)
13 ) where
14
15 import {-# SOURCE #-} HaddockLex
16 import HsSyn
17 import RdrName
18 }
19
20 %tokentype { Token }
21
22 %token  '/'     { TokSpecial '/' }
23         '@'     { TokSpecial '@' }
24         '['     { TokDefStart }
25         ']'     { TokDefEnd }
26         DQUO    { TokSpecial '\"' }
27         URL     { TokURL $$ }
28         PIC     { TokPic $$ }
29         ANAME   { TokAName $$ }
30         '/../'  { TokEmphasis $$ }
31         '-'     { TokBullet }
32         '(n)'   { TokNumber }
33         '>..'   { TokBirdTrack $$ }
34         IDENT   { TokIdent $$ }
35         PARA    { TokPara }
36         STRING  { TokString $$ }
37
38 %monad { MyEither String }
39
40 %name parseHaddockParagraphs  doc
41 %name parseHaddockString seq
42
43 %%
44
45 doc     :: { HsDoc RdrName }
46         : apara PARA doc        { docAppend $1 $3 }
47         | PARA doc              { $2 }
48         | apara                 { $1 }
49         | {- empty -}           { DocEmpty }
50
51 apara   :: { HsDoc RdrName }
52         : ulpara                { DocUnorderedList [$1] }
53         | olpara                { DocOrderedList [$1] }
54         | defpara               { DocDefList [$1] }
55         | para                  { $1 }
56
57 ulpara  :: { HsDoc RdrName }
58         : '-' para              { $2 }
59
60 olpara  :: { HsDoc RdrName } 
61         : '(n)' para            { $2 }
62
63 defpara :: { (HsDoc RdrName, HsDoc RdrName) }
64         : '[' seq ']' seq       { ($2, $4) }
65
66 para    :: { HsDoc RdrName }
67         : seq                   { docParagraph $1 }
68         | codepara              { DocCodeBlock $1 }
69
70 codepara :: { HsDoc RdrName }
71         : '>..' codepara        { docAppend (DocString $1) $2 }
72         | '>..'                 { DocString $1 }
73
74 seq     :: { HsDoc RdrName }
75         : elem seq              { docAppend $1 $2 }
76         | elem                  { $1 }
77
78 elem    :: { HsDoc RdrName }
79         : elem1                 { $1 }
80         | '@' seq1 '@'          { DocMonospaced $2 }
81
82 seq1    :: { HsDoc RdrName }
83         : PARA seq1             { docAppend (DocString "\n") $2 }
84         | elem1 seq1            { docAppend $1 $2 }
85         | elem1                 { $1 }
86
87 elem1   :: { HsDoc RdrName }
88         : STRING                { DocString $1 }
89         | '/../'                { DocEmphasis (DocString $1) }
90         | URL                   { DocURL $1 }
91         | PIC                   { DocPic $1 }
92         | ANAME                 { DocAName $1 }
93         | IDENT                 { DocIdentifier $1 }
94         | DQUO strings DQUO     { DocModule $2 }
95
96 strings  :: { String }
97         : STRING                { $1 }
98         | STRING strings        { $1 ++ $2 }
99
100 {
101 happyError :: [Token] -> MyEither String a
102 happyError toks = MyLeft ("parse error in doc string")
103
104 -- We don't want to make an instance for Either String,
105 -- since every user of the GHC API would get that instance
106
107 data MyEither a b = MyLeft a | MyRight b
108
109 instance Monad (MyEither String) where
110         return          = MyRight
111         MyLeft  l >>= _ = MyLeft l
112         MyRight r >>= k = k r
113         fail msg        = MyLeft msg
114 }