2 -- Haddock - A Haskell Documentation Tool
4 -- (c) Simon Marlow 2002
6 -- This file was modified and integrated into GHC by David Waern 2006
16 import Lexer hiding (Token)
17 import Parser ( parseIdentifier )
27 import System.IO.Unsafe
32 $hexdigit = [0-9a-fA-F]
34 $alphanum = [A-Za-z0-9]
35 $ident = [$alphanum \'\_\.\!\#\$\%\&\*\+\/\<\=\>\?\@\\\\\^\|\-\~]
39 -- beginning of a paragraph
42 $ws* \> { begin birdtrack }
43 $ws* [\*\-] { token TokBullet `andBegin` string }
44 $ws* \[ { token TokDefStart `andBegin` def }
45 $ws* \( $digit+ \) { token TokNumber `andBegin` string }
49 -- beginning of a line
51 $ws* \> { begin birdtrack }
52 $ws* \n { token TokPara `andBegin` para }
53 -- Here, we really want to be able to say
54 -- $ws* (\n | <eof>) { token TokPara `andBegin` para}
55 -- because otherwise a trailing line of whitespace will result in
56 -- a spurious TokString at the end of a docstring. We don't have <eof>,
57 -- though (NOW I realise what it was for :-). To get around this, we always
58 -- append \n to the end of a docstring.
62 <birdtrack> .* \n? { strtoken TokBirdTrack `andBegin` line }
65 $special { strtoken $ \s -> TokSpecial (head s) }
66 \<.*\> { strtoken $ \s -> TokURL (init (tail s)) }
67 \#.*\# { strtoken $ \s -> TokAName (init (tail s)) }
68 [\'\`] $ident+ [\'\`] { ident }
69 \\ . { strtoken (TokString . tail) }
70 "&#" $digit+ \; { strtoken $ \s -> TokString [chr (read (init (drop 2 s)))] }
71 "&#" [xX] $hexdigit+ \; { strtoken $ \s -> case readHex (init (drop 3 s)) of [(n,_)] -> TokString [chr n] }
72 -- allow special characters through if they don't fit one of the previous
74 [\'\`\<\#\&\\] { strtoken TokString }
75 [^ $special \< \# \n \'\` \& \\ \]]* \n { strtoken TokString `andBegin` line }
76 [^ $special \< \# \n \'\` \& \\ \]]+ { strtoken TokString }
80 \] { token TokDefEnd `andBegin` string }
83 -- ']' doesn't have any special meaning outside of the [...] at the beginning
84 -- of a definition paragraph.
86 \] { strtoken TokString }
101 | TokBirdTrack String
104 -- -----------------------------------------------------------------------------
105 -- Alex support stuff
108 type Action = String -> StartCode -> (StartCode -> [Token]) -> [Token]
110 type AlexInput = (Char,String)
112 alexGetChar (_, []) = Nothing
113 alexGetChar (_, c:cs) = Just (c, (c,cs))
115 alexInputPrevChar (c,_) = c
117 tokenise :: String -> [Token]
118 tokenise str = let toks = go ('\n', eofHack str) para in {-trace (show toks)-} toks
119 where go inp@(_,str) sc =
120 case alexScan inp sc of
122 AlexError _ -> error "lexical error"
123 AlexSkip inp' len -> go inp' sc
124 AlexToken inp' len act -> act (take len str) sc (\sc -> go inp' sc)
126 -- NB. we add a final \n to the string, (see comment in the beginning of line
127 -- production above).
128 eofHack str = str++"\n"
130 andBegin :: Action -> StartCode -> Action
131 andBegin act new_sc = \str sc cont -> act str new_sc cont
133 token :: Token -> Action
134 token t = \str sc cont -> t : cont sc
136 strtoken :: (String -> Token) -> Action
137 strtoken t = \str sc cont -> t str : cont sc
139 begin :: StartCode -> Action
140 begin sc = \str _ cont -> cont sc
142 -- -----------------------------------------------------------------------------
143 -- Lex a string as a Haskell identifier
147 case strToHsQNames id of
148 Just names -> TokIdent names : cont sc
149 Nothing -> TokString str : cont sc
150 where id = init (tail str)
152 strToHsQNames :: String -> Maybe [RdrName]
154 let buffer = unsafePerformIO (stringToStringBuffer str0)
155 pstate = mkPState buffer noSrcLoc defaultDynFlags
156 lex = lexer (\t -> return t)
157 result = unP parseIdentifier pstate
159 POk _ name -> Just [unLoc name]