| 1 |
-- -*- haskell -*- |
| 2 |
-- |
| 3 |
-- Lexical syntax for literate Haskell 98. |
| 4 |
-- |
| 5 |
-- (c) Simon Marlow 2003, with the caveat that much of this is |
| 6 |
-- translated directly from the syntax in the Haskell 98 report. |
| 7 |
-- |
| 8 |
-- Adapted to literate Haskell 98 by Nicolas Pouillard |
| 9 |
-- |
| 10 |
|
| 11 |
{ |
| 12 |
{-# OPTIONS -w #-} |
| 13 |
module Yi.Lexer.LiterateHaskell ( initState, alexScanToken, HlState ) where |
| 14 |
import Yi.Lexer.Alex hiding (tokenToStyle) |
| 15 |
import Yi.Lexer.Haskell hiding (initState, alexScanToken, HlState) |
| 16 |
import Yi.Style |
| 17 |
} |
| 18 |
|
| 19 |
$whitechar = [\ \t\n\r\f\v] |
| 20 |
$special = [\(\)\,\;\[\]\`\{\}] |
| 21 |
|
| 22 |
$ascdigit = 0-9 |
| 23 |
$unidigit = [] -- TODO |
| 24 |
$digit = [$ascdigit $unidigit] |
| 25 |
|
| 26 |
$ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~] |
| 27 |
$unisymbol = [] -- TODO |
| 28 |
$symbol = [$ascsymbol $unisymbol] # [$special \_\:\"\'] |
| 29 |
|
| 30 |
$large = [A-Z \xc0-\xd6 \xd8-\xde] |
| 31 |
$small = [a-z \xdf-\xf6 \xf8-\xff \_] |
| 32 |
$alpha = [$small $large] |
| 33 |
|
| 34 |
$graphic = [$small $large $symbol $digit $special \:\"\'] |
| 35 |
|
| 36 |
$octit = 0-7 |
| 37 |
$hexit = [0-9 A-F a-f] |
| 38 |
$idchar = [$alpha $digit \'] |
| 39 |
$symchar = [$symbol \:] |
| 40 |
$nl = [\n\r] |
| 41 |
|
| 42 |
@reservedid = |
| 43 |
as|case|class|data|default|else|hiding|if| |
| 44 |
import|in|infix|infixl|infixr|instance|newtype| |
| 45 |
qualified|then|type|family|foreign|export|dynamic| |
| 46 |
safe|threadsafe|unsafe|stdcall|ccall|dotnet |
| 47 |
|
| 48 |
@layoutReservedId = |
| 49 |
of|let|do|mdo |
| 50 |
|
| 51 |
|
| 52 |
@reservedop = |
| 53 |
".." | ":" | "::" | "=" | \\ | "|" | "<-" | "->" | "@" | "~" | "=>" |
| 54 |
|
| 55 |
@varid = $small $idchar* |
| 56 |
@conid = $large $idchar* |
| 57 |
@qual = (@conid ".")* |
| 58 |
@varsym = $symbol $symchar* |
| 59 |
@consym = \: $symchar* |
| 60 |
|
| 61 |
@decimal = $digit+ |
| 62 |
@octal = $octit+ |
| 63 |
@hexadecimal = $hexit+ |
| 64 |
@exponent = [eE] [\-\+] @decimal |
| 65 |
|
| 66 |
$cntrl = [$large \@\[\\\]\^\_] |
| 67 |
@ascii = \^ $cntrl | NUL | SOH | STX | ETX | EOT | ENQ | ACK |
| 68 |
| BEL | BS | HT | LF | VT | FF | CR | SO | SI | DLE |
| 69 |
| DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM |
| 70 |
| SUB | ESC | FS | GS | RS | US | SP | DEL |
| 71 |
$charesc = [abfnrtv\\\"\'\&] |
| 72 |
@escape = \\ ($charesc | @ascii | @decimal | o @octal | x @hexadecimal) |
| 73 |
@gap = \\ $whitechar+ \\ |
| 74 |
@string = $graphic # [\"\\] | " " | @escape | @gap |
| 75 |
|
| 76 |
haskell :- |
| 77 |
|
| 78 |
<0> $white+ ; |
| 79 |
|
| 80 |
<nestcomm> { |
| 81 |
"{-" { m CommentBlock (Comment Open) } |
| 82 |
"-}" { m unComment (Comment Close) } |
| 83 |
$white+ ; -- whitespace |
| 84 |
[^\-\{]+ { c $ Comment Text } -- rule to generate comments larger than 1 char |
| 85 |
. { c $ Comment Text } |
| 86 |
} |
| 87 |
|
| 88 |
<0> { |
| 89 |
^ "\begin{code}" { m (const CodeBlock) $ Reserved Other } |
| 90 |
^ ">" { ms (const CodeLine) Operator } |
| 91 |
$white+ ; -- whitespace |
| 92 |
. { c $ Comment Text {-LaTeX-} } |
| 93 |
} |
| 94 |
|
| 95 |
<codeBlock> { |
| 96 |
"\end{code}" { m (const LaTeXBlock) $ Reserved Other } |
| 97 |
|
| 98 |
$white+ ; -- whitespace |
| 99 |
|
| 100 |
-- The first rule matches operators that begin with --, eg --++-- is a valid |
| 101 |
-- operator and *not* a comment. |
| 102 |
-- Note that we have to dissallow '-' as a symbol char for the first one |
| 103 |
-- of these because we may have -------- which would stilljust be the |
| 104 |
-- start of a comment. |
| 105 |
"--"\-* [$symbol # \-] $symchar* { cs Operator } |
| 106 |
-- The next rule allows for the start of a comment basically |
| 107 |
-- it is -- followed by anything which isn't a symbol character |
| 108 |
-- (OR more '-'s). So for example "-----:" is still the start of a comment. |
| 109 |
"--"~[$symbol # \-][^$nl]* { c $ Comment Line } |
| 110 |
-- Finally because the above rule had to add in a non symbol character |
| 111 |
-- it's also possible that we have just finishing a line, |
| 112 |
-- people sometimes do this for example when breaking up paragraphs |
| 113 |
-- in a long comment. |
| 114 |
"--"$nl { c $ Comment Line } |
| 115 |
|
| 116 |
"{-" { m CommentBlock $ Comment Open } |
| 117 |
|
| 118 |
^"#".* { c $ CppDirective } |
| 119 |
$special { cs $ \(c:_) -> Special c } |
| 120 |
"deriving" { c (Reserved Deriving) } |
| 121 |
"forall" { c (Reserved Forall) } |
| 122 |
@reservedid { c (Reserved Other) } |
| 123 |
"module" { c (Reserved Module) } |
| 124 |
"where" { c (Reserved Where) } |
| 125 |
@layoutReservedId { c (Reserved OtherLayout) } |
| 126 |
`@qual @varid` { cs $ Operator . init . tail } |
| 127 |
`@qual @conid` { cs $ ConsOperator . init . tail } |
| 128 |
@qual @varid { c VarIdent } |
| 129 |
@qual @conid { c ConsIdent } |
| 130 |
|
| 131 |
"|" { c (ReservedOp Pipe) } |
| 132 |
"=" { c (ReservedOp Equal) } |
| 133 |
\\ { c (ReservedOp BackSlash) } |
| 134 |
"<-" { c (ReservedOp LeftArrow) } |
| 135 |
"->" { c (ReservedOp RightArrow) } |
| 136 |
"=>" { c (ReservedOp DoubleRightArrow) } |
| 137 |
".." { c (ReservedOp DoubleDot) } |
| 138 |
"@" { c (ReservedOp Arobase) } |
| 139 |
"~" { c (ReservedOp Tilda) } |
| 140 |
"=>" { c (ReservedOp DoubleRightArrow) } |
| 141 |
"::" { c (ReservedOp DoubleColon) } |
| 142 |
@qual @varsym { cs Operator } |
| 143 |
@qual @consym { cs ConsOperator } |
| 144 |
|
| 145 |
@decimal |
| 146 |
| 0[oO] @octal |
| 147 |
| 0[xX] @hexadecimal { c Number } |
| 148 |
|
| 149 |
@decimal \. @decimal @exponent? |
| 150 |
| @decimal @exponent { c Number } |
| 151 |
|
| 152 |
\' ($graphic # [\'\\] | " " | @escape) \' { c CharTok } |
| 153 |
\" @string* \" { c StringTok } |
| 154 |
. { c Unrecognized } |
| 155 |
} |
| 156 |
|
| 157 |
<codeLine> { |
| 158 |
[\t\n\r\f\v]+ { m (const LaTeXBlock) $ Reserved Other } |
| 159 |
|
| 160 |
[\ \t]+ ; -- whitespace |
| 161 |
|
| 162 |
-- Same three rules for line comments as above (see above for explanation). |
| 163 |
"--"\-* [$symbol # \-] $symchar* { cs Operator } |
| 164 |
"--"~[$symbol # \-][^$nl]* { c $ Comment Line } |
| 165 |
"--"$nl { c $ Comment Line } |
| 166 |
|
| 167 |
"{-" { m CommentBlock $ Comment Open } |
| 168 |
|
| 169 |
^"#".* { c $ CppDirective } |
| 170 |
$special { cs $ \(c:_) -> Special c } |
| 171 |
"deriving" { c (Reserved Deriving) } |
| 172 |
"forall" { c (Reserved Forall) } |
| 173 |
@reservedid { c (Reserved Other) } |
| 174 |
"module" { c (Reserved Module) } |
| 175 |
"where" { c (Reserved Where) } |
| 176 |
@layoutReservedId { c (Reserved OtherLayout) } |
| 177 |
`@qual @varid` { cs $ Operator . init . tail } |
| 178 |
`@qual @conid` { cs $ ConsOperator . init . tail } |
| 179 |
@qual @varid { c VarIdent } |
| 180 |
@qual @conid { c ConsIdent } |
| 181 |
|
| 182 |
"|" { c (ReservedOp Pipe) } |
| 183 |
"=" { c (ReservedOp Equal) } |
| 184 |
\\ { c (ReservedOp BackSlash) } |
| 185 |
"<-" { c (ReservedOp LeftArrow) } |
| 186 |
"->" { c (ReservedOp RightArrow) } |
| 187 |
"=>" { c (ReservedOp DoubleRightArrow) } |
| 188 |
".." { c (ReservedOp DoubleDot) } |
| 189 |
"@" { c (ReservedOp Arobase) } |
| 190 |
"~" { c (ReservedOp Tilda) } |
| 191 |
"=>" { c (ReservedOp DoubleRightArrow) } |
| 192 |
"::" { c (ReservedOp DoubleColon) } |
| 193 |
@qual @varsym { cs Operator } |
| 194 |
@qual @consym { cs ConsOperator } |
| 195 |
|
| 196 |
@decimal |
| 197 |
| 0[oO] @octal |
| 198 |
| 0[xX] @hexadecimal { c Number } |
| 199 |
|
| 200 |
@decimal \. @decimal @exponent? |
| 201 |
| @decimal @exponent { c Number } |
| 202 |
|
| 203 |
\' ($graphic # [\'\\] | " " | @escape) \' { c CharTok } |
| 204 |
\" @string* \" { c StringTok } |
| 205 |
. { c Unrecognized } |
| 206 |
} |
| 207 |
|
| 208 |
{ |
| 209 |
|
| 210 |
data HlState = CodeBlock |
| 211 |
| CodeLine |
| 212 |
| CommentBlock { unComment :: HlState } |
| 213 |
| LaTeXBlock |
| 214 |
deriving (Eq, Show) |
| 215 |
|
| 216 |
stateToInit (CommentBlock _) = nestcomm |
| 217 |
stateToInit CodeBlock = codeBlock |
| 218 |
stateToInit CodeLine = codeLine |
| 219 |
stateToInit LaTeXBlock = 0 |
| 220 |
|
| 221 |
initState = LaTeXBlock |
| 222 |
|
| 223 |
#include "common.hsinc" |
| 224 |
} |