HaddockParse.y 2.79 KB
Newer Older
1
{
2
{-# OPTIONS -Wwarn -w #-}
3
4
5
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
Ian Lynagh's avatar
Ian Lynagh committed
6
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
7
8
-- for details

9
10
11
module HaddockParse (
  parseHaddockParagraphs, 
  parseHaddockString, 
12
  EitherString(..)
13
) where
14
15
16
17
18
19

import {-# SOURCE #-} HaddockLex
import HsSyn
import RdrName
}

20
21
%expect 0

22
23
24
25
26
27
28
29
%tokentype { Token }

%token	'/'	{ TokSpecial '/' }
	'@'	{ TokSpecial '@' }
	'['     { TokDefStart }
	']'     { TokDefEnd }
	DQUO 	{ TokSpecial '\"' }
	URL	{ TokURL $$ }
30
	PIC     { TokPic $$ }
31
	ANAME	{ TokAName $$ }
32
	'/../'  { TokEmphasis $$ }
33
34
35
36
37
38
39
	'-'	{ TokBullet }
	'(n)'	{ TokNumber }
	'>..'	{ TokBirdTrack $$ }
	IDENT   { TokIdent $$ }
	PARA    { TokPara }
	STRING	{ TokString $$ }

40
%monad { EitherString }
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

%name parseHaddockParagraphs  doc
%name parseHaddockString seq

%%

doc	:: { HsDoc RdrName }
	: apara PARA doc	{ docAppend $1 $3 }
	| PARA doc 		{ $2 }
	| apara			{ $1 }
	| {- empty -}		{ DocEmpty }

apara	:: { HsDoc RdrName }
	: ulpara		{ DocUnorderedList [$1] }
	| olpara		{ DocOrderedList [$1] }
        | defpara               { DocDefList [$1] }
	| para			{ $1 }

ulpara  :: { HsDoc RdrName }
	: '-' para		{ $2 }

olpara  :: { HsDoc RdrName } 
	: '(n)' para		{ $2 }

defpara :: { (HsDoc RdrName, HsDoc RdrName) }
	: '[' seq ']' seq	{ ($2, $4) }

para    :: { HsDoc RdrName }
	: seq			{ docParagraph $1 }
	| codepara		{ DocCodeBlock $1 }

codepara :: { HsDoc RdrName }
	: '>..' codepara	{ docAppend (DocString $1) $2 }
	| '>..'			{ DocString $1 }

seq	:: { HsDoc RdrName }
	: elem seq		{ docAppend $1 $2 }
	| elem			{ $1 }

elem	:: { HsDoc RdrName }
	: elem1			{ $1 }
	| '@' seq1 '@'		{ DocMonospaced $2 }

seq1	:: { HsDoc RdrName }
85
86
	: PARA seq1             { docAppend (DocString "\n") $2 }
	| elem1 seq1            { docAppend $1 $2 }
87
88
89
90
	| elem1			{ $1 }

elem1	:: { HsDoc RdrName }
	: STRING		{ DocString $1 }
91
	| '/../'                { DocEmphasis (DocString $1) }
92
	| URL			{ DocURL $1 }
93
	| PIC                   { DocPic $1 }
94
95
96
97
98
99
100
101
102
	| ANAME			{ DocAName $1 }
	| IDENT			{ DocIdentifier $1 }
	| DQUO strings DQUO	{ DocModule $2 }

strings  :: { String }
	: STRING		{ $1 }
	| STRING strings	{ $1 ++ $2 }

{
103
happyError :: [Token] -> EitherString a
104
105
106
107
108
happyError toks = MyLeft ("parse error in doc string")

-- We don't want to make an instance for Either String,
-- since every user of the GHC API would get that instance

109
110
111
112
-- But why use non-Haskell98 instances when MyEither String
-- is the only MyEither we're intending to use anyway? --Isaac Dupree
--data MyEither a b = MyLeft a | MyRight b
data EitherString b = MyLeft String | MyRight b
113

114
instance Monad EitherString where
115
116
117
118
	return          = MyRight
	MyLeft  l >>= _ = MyLeft l
	MyRight r >>= k = k r
	fail msg        = MyLeft msg
119
}