Merge from Haddock: Add <<url>> for images
[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 (parseHaddockParagraphs, parseHaddockString) where
10
11 import {-# SOURCE #-} HaddockLex
12 import HsSyn
13 import RdrName
14 }
15
16 %tokentype { Token }
17
18 %token  '/'     { TokSpecial '/' }
19         '@'     { TokSpecial '@' }
20         '['     { TokDefStart }
21         ']'     { TokDefEnd }
22         DQUO    { TokSpecial '\"' }
23         URL     { TokURL $$ }
24         PIC     { TokPic $$ }
25         ANAME   { TokAName $$ }
26         '/../'  { TokEmphasis $$ }
27         '-'     { TokBullet }
28         '(n)'   { TokNumber }
29         '>..'   { TokBirdTrack $$ }
30         IDENT   { TokIdent $$ }
31         PARA    { TokPara }
32         STRING  { TokString $$ }
33
34 %monad { Either String }
35
36 %name parseHaddockParagraphs  doc
37 %name parseHaddockString seq
38
39 %%
40
41 doc     :: { HsDoc RdrName }
42         : apara PARA doc        { docAppend $1 $3 }
43         | PARA doc              { $2 }
44         | apara                 { $1 }
45         | {- empty -}           { DocEmpty }
46
47 apara   :: { HsDoc RdrName }
48         : ulpara                { DocUnorderedList [$1] }
49         | olpara                { DocOrderedList [$1] }
50         | defpara               { DocDefList [$1] }
51         | para                  { $1 }
52
53 ulpara  :: { HsDoc RdrName }
54         : '-' para              { $2 }
55
56 olpara  :: { HsDoc RdrName } 
57         : '(n)' para            { $2 }
58
59 defpara :: { (HsDoc RdrName, HsDoc RdrName) }
60         : '[' seq ']' seq       { ($2, $4) }
61
62 para    :: { HsDoc RdrName }
63         : seq                   { docParagraph $1 }
64         | codepara              { DocCodeBlock $1 }
65
66 codepara :: { HsDoc RdrName }
67         : '>..' codepara        { docAppend (DocString $1) $2 }
68         | '>..'                 { DocString $1 }
69
70 seq     :: { HsDoc RdrName }
71         : elem seq              { docAppend $1 $2 }
72         | elem                  { $1 }
73
74 elem    :: { HsDoc RdrName }
75         : elem1                 { $1 }
76         | '@' seq1 '@'          { DocMonospaced $2 }
77
78 seq1    :: { HsDoc RdrName }
79         : PARA seq1             { docAppend (DocString "\n") $2 }
80         | elem1 seq1            { docAppend $1 $2 }
81         | elem1                 { $1 }
82
83 elem1   :: { HsDoc RdrName }
84         : STRING                { DocString $1 }
85         | '/../'                { DocEmphasis (DocString $1) }
86         | URL                   { DocURL $1 }
87         | PIC                   { DocPic $1 }
88         | ANAME                 { DocAName $1 }
89         | IDENT                 { DocIdentifier $1 }
90         | DQUO strings DQUO     { DocModule $2 }
91
92 strings  :: { String }
93         : STRING                { $1 }
94         | STRING strings        { $1 ++ $2 }
95
96 {
97 happyError :: [Token] -> Either String a
98 happyError toks = 
99 --  Left ("parse error in doc string: "  ++ show (take 3 toks))
100   Left ("parse error in doc string")
101
102 -- Either monad (we can't use MonadError because GHC < 5.00 has
103 -- an older incompatible version).
104 instance Monad (Either String) where
105         return        = Right
106         Left  l >>= _ = Left l
107         Right r >>= k = k r
108         fail msg      = Left msg
109 }