1
{- This module was generated from data in the Kate syntax highlighting file d.xml, version 1.43,
2
by Aziz K�ksal (aziz.koeksal@gmail.com), Jari-Matti M�kel� (jmjm@iki.fi), Simon J Mackenzie (project.katedxml@smackoz.fastmail.fm) -}
4
module Text.Highlighting.Kate.Syntax.D ( highlight, parseExpression, syntaxName, syntaxExtensions ) where
5
import Text.Highlighting.Kate.Definitions
6
import Text.Highlighting.Kate.Common
7
import Text.ParserCombinators.Parsec
9
import Data.Map (fromList)
10
import Data.Maybe (fromMaybe)
15
syntaxExtensions :: String
16
syntaxExtensions = "*.d;*.D;*.di;*.DI;"
18
highlight :: String -> Either String [SourceLine]
20
case runParser parseSource startingState "source" input of
21
Left err -> Left $ show err
22
Right result -> Right result
24
parseExpression :: GenParser Char SyntaxState LabeledSource
27
let oldLang = synStLanguage st
28
setState $ st { synStLanguage = "D" }
29
context <- currentContext <|> (pushContext "normal" >> currentContext)
30
result <- parseRules context
31
updateState $ \st -> st { synStLanguage = oldLang }
35
lineContents <- lookAhead wholeLine
36
updateState $ \st -> st { synStCurrentLine = lineContents }
37
result <- manyTill parseSourceLine eof
38
return $ map normalizeHighlighting result
40
startingState = SyntaxState {synStContexts = fromList [("D",["normal"])], synStLanguage = "D", synStCurrentLine = "", synStCharsParsedInLine = 0, synStCaseSensitive = True, synStKeywordCaseSensitive = True, synStCaptures = []}
42
parseSourceLine = manyTill parseExpressionInternal pEndLine
45
newline <|> (eof >> return '\n')
46
context <- currentContext
49
"UnicodeShort" -> (popContext >> return ())
50
"UnicodeLong" -> (popContext >> return ())
51
"HTMLEntity" -> (popContext >> return ())
52
"ModuleName" -> return ()
53
"Deprecated" -> return ()
54
"Linkage" -> (popContext >> return ())
55
"Linkage2" -> (popContext >> return ())
56
"Version" -> (popContext >> return ())
57
"Version2" -> (popContext >> return ())
58
"Pragmas" -> (popContext >> return ())
59
"RawString" -> return ()
60
"BQString" -> return ()
61
"HexString" -> return ()
62
"CharLiteral" -> (popContext >> return ())
64
"CommentLine" -> (popContext >> return ())
65
"CommentBlock" -> return ()
66
"CommentNested" -> return ()
68
lineContents <- lookAhead wholeLine
69
updateState $ \st -> st { synStCurrentLine = lineContents, synStCharsParsedInLine = 0 }
71
withAttribute attr txt = do
73
then fail "Parser matched no text"
75
let style = fromMaybe "" $ lookup attr styles
77
let oldCharsParsed = synStCharsParsedInLine st
78
updateState $ \st -> st { synStCharsParsedInLine = oldCharsParsed + length txt }
79
return (nub [style, attr], txt)
81
styles = [("Normal Text","Normal"),("Keyword","Keyword"),("Type","DataType"),("Integer","DecVal"),("Binary","BaseN"),("Octal","BaseN"),("Hex","BaseN"),("Float","Float"),("LibrarySymbols","DataType"),("Deprecated","Comment"),("SpecialTokens","Normal"),("Module","Keyword"),("Module Name","Normal"),("Linkage","Keyword"),("Linkage Type","Normal"),("Debug","Keyword"),("Assert","Keyword"),("Version","Keyword"),("Version Type","Normal"),("Unit Test","Keyword"),("Pragma","Keyword"),("EscapeString","String"),("EscapeSequence","String"),("String","String"),("Char","Char"),("RawString","String"),("BQString","String"),("HexString","String"),("Comment","Comment"),("Error","Error")]
83
parseExpressionInternal = do
84
context <- currentContext
85
parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes))
87
defaultAttributes = [("normal","Normal Text"),("UnicodeShort","EscapeString"),("UnicodeLong","EscapeString"),("HTMLEntity","EscapeString"),("ModuleName","Module Name"),("Deprecated","Deprecated"),("Linkage","Linkage"),("Linkage2","Linkage"),("Version","Version"),("Version2","Version"),("Pragmas","Pragma"),("RawString","RawString"),("BQString","BQString"),("HexString","HexString"),("CharLiteral","Char"),("String","String"),("CommentLine","Comment"),("CommentBlock","Comment"),("CommentNested","Comment")]
90
do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["abstract","alias","align","asm","auto","body","break","case","cast","catch","class","const","continue","default","delegate","delete","do","else","enum","export","false","final","finally","for","foreach","foreach_reverse","function","goto","if","in","inout","interface","invariant","is","lazy","macro","mixin","new","null","out","override","package","private","protected","public","ref","return","scope","static","struct","super","switch","synchronized","template","this","throw","true","try","typedef","typeid","typeof","union","volatile","while","with"] >>= withAttribute "Keyword"))
92
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["module","import"] >>= withAttribute "Module") >>~ pushContext "ModuleName")
94
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["void","bool","byte","ubyte","short","ushort","int","uint","long","ulong","cent","ucent","float","double","real","ireal","ifloat","idouble","creal","cfloat","cdouble","char","wchar","dchar"] >>= withAttribute "Type"))
96
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["string","wstring","dstring","size_t","ptrdiff_t","hash_t","Error","Exception","Object","TypeInfo","ClassInfo"] >>= withAttribute "LibrarySymbols"))
98
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["extern"] >>= withAttribute "Linkage") >>~ pushContext "Linkage")
100
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["debug"] >>= withAttribute "Debug"))
102
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["assert"] >>= withAttribute "Assert"))
104
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["pragma"] >>= withAttribute "Pragma") >>~ pushContext "Pragmas")
106
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["version"] >>= withAttribute "Version") >>~ pushContext "Version")
108
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["unittest"] >>= withAttribute "Unit Test"))
110
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["__FILE__","__LINE__","__DATE__","__TIME__","__TIMESTAMP__","__VENDOR__","__VERSION__","__EOF__"] >>= withAttribute "SpecialTokens"))
112
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["deprecated"] >>= withAttribute "Deprecated") >>~ pushContext "Deprecated")
114
((pDetect2Chars False 'r' '"' >>= withAttribute "RawString") >>~ pushContext "RawString")
116
((pDetect2Chars False 'x' '"' >>= withAttribute "HexString") >>~ pushContext "HexString")
118
((pDetectIdentifier >>= withAttribute "Normal Text"))
120
((pHlCStringChar >>= withAttribute "EscapeString") >>~ (popContext >> return ()))
122
((pDetect2Chars False '\\' 'u' >>= withAttribute "EscapeString") >>~ pushContext "UnicodeShort")
124
((pDetect2Chars False '\\' 'U' >>= withAttribute "EscapeString") >>~ pushContext "UnicodeLong")
126
((pDetect2Chars False '\\' '&' >>= withAttribute "EscapeString") >>~ pushContext "HTMLEntity")
128
((pDetectChar False '\'' >>= withAttribute "Char") >>~ pushContext "CharLiteral")
130
((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")
132
((pDetectChar False '`' >>= withAttribute "BQString") >>~ pushContext "BQString")
134
((pDetect2Chars False '/' '/' >>= withAttribute "Comment") >>~ pushContext "CommentLine")
136
((pDetect2Chars False '/' '*' >>= withAttribute "Comment") >>~ pushContext "CommentBlock")
138
((pDetect2Chars False '/' '+' >>= withAttribute "Comment") >>~ pushContext "CommentNested")
140
((pDetectChar False '{' >>= withAttribute "Normal Text"))
142
((pDetectChar False '}' >>= withAttribute "Normal Text"))
144
((pString False "..." >>= withAttribute "Normal Text") >>~ (popContext >> return ()))
146
((pDetect2Chars False '.' '.' >>= withAttribute "Normal Text"))
148
((pRegExpr (compileRegex "0[xX][_a-fA-F\\d]*(\\.[_a-fA-F\\d]*)?[pP][-+]?[\\d]+[_\\d]*[fFL]?i?") >>= withAttribute "Float") >>~ (popContext >> return ()))
150
((pRegExpr (compileRegex "[\\d][_\\d]*(\\.(?!\\.)[_\\d]*([eE][-+]?[\\d]+[_\\d]*)?[fFL]?i?|[eE][-+]?[\\d]+[_\\d]*[fFL]?i?|[fF]i?|[fFL]?i)") >>= withAttribute "Float") >>~ (popContext >> return ()))
152
((pRegExpr (compileRegex "\\.[\\d][_\\d]*([eE][-+]?[\\d]+[_\\d]*)?[fFL]?i?") >>= withAttribute "Float") >>~ (popContext >> return ()))
154
((pRegExpr (compileRegex "0[bB]_*[01][01_]*(L[uU]?|[uU]L?)?") >>= withAttribute "Binary") >>~ (popContext >> return ()))
156
((pRegExpr (compileRegex "0_*[0-7][0-7_]*(L[uU]?|[uU]L?)?") >>= withAttribute "Octal") >>~ (popContext >> return ()))
158
((pRegExpr (compileRegex "0[xX]_*[\\da-fA-F][\\da-fA-F_]*(L[uU]?|[uU]L?)?") >>= withAttribute "Hex") >>~ (popContext >> return ()))
160
((pRegExpr (compileRegex "\\d+[\\d_]*(L[uU]?|[uU]L?)?") >>= withAttribute "Integer") >>~ (popContext >> return ()))
162
((pString False "#line" >>= withAttribute "Pragma") >>~ (popContext >> return ())))
163
return (attr, result)
165
parseRules "UnicodeShort" =
166
do (attr, result) <- ((pRegExpr (compileRegex "[\\da-fA-F]{4}") >>= withAttribute "EscapeString") >>~ (popContext >> return ()))
167
return (attr, result)
169
parseRules "UnicodeLong" =
170
do (attr, result) <- ((pRegExpr (compileRegex "[\\da-fA-F]{8}") >>= withAttribute "EscapeString") >>~ (popContext >> return ()))
171
return (attr, result)
173
parseRules "HTMLEntity" =
174
do (attr, result) <- (((pRegExpr (compileRegex "[a-zA-Z]\\w+;") >>= withAttribute "EscapeString") >>~ (popContext >> return ()))
176
((popContext >> return ()) >> return ([], "")))
177
return (attr, result)
179
parseRules "ModuleName" =
180
do (attr, result) <- (((pDetect2Chars False '/' '/' >>= withAttribute "Comment") >>~ pushContext "CommentLine")
182
((pDetect2Chars False '/' '*' >>= withAttribute "Comment") >>~ pushContext "CommentBlock")
184
((pDetect2Chars False '/' '+' >>= withAttribute "Comment") >>~ pushContext "CommentNested")
186
((pRegExpr (compileRegex "[^\\s\\w.:,]") >>= withAttribute "Module Name") >>~ (popContext >> return ())))
187
return (attr, result)
189
parseRules "Deprecated" =
190
do (attr, result) <- (((pDetect2Chars False '/' '/' >>= withAttribute "Comment") >>~ pushContext "CommentLine")
192
((pDetect2Chars False '/' '*' >>= withAttribute "Comment") >>~ pushContext "CommentBlock")
194
((pDetect2Chars False '/' '+' >>= withAttribute "Comment") >>~ pushContext "CommentNested")
196
((pRegExpr (compileRegex "[;({=]") >>= withAttribute "Normal Text") >>~ (popContext >> return ())))
197
return (attr, result)
199
parseRules "Linkage" =
200
do (attr, result) <- (((pDetectSpaces >>= withAttribute "Linkage"))
202
((pDetectChar False '(' >>= withAttribute "Normal Text") >>~ pushContext "Linkage2")
204
((popContext >> return ()) >> return ([], "")))
205
return (attr, result)
207
parseRules "Linkage2" =
208
do (attr, result) <- (((pDetectSpaces >>= withAttribute "Linkage"))
210
((pString False "C++" >>= withAttribute "Linkage Type") >>~ (popContext >> return ()))
212
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["C","D","Windows","Pascal","System"] >>= withAttribute "Linkage Type") >>~ (popContext >> return ()))
214
((pRegExpr (compileRegex "[^)]+") >>= withAttribute "Error") >>~ (popContext >> return ()))
216
((popContext >> return ()) >> return ([], "")))
217
return (attr, result)
219
parseRules "Version" =
220
do (attr, result) <- (((pDetectSpaces >>= withAttribute "Version"))
222
((pDetectChar False '=' >>= withAttribute "Normal Text") >>~ pushContext "Version2")
224
((pDetectChar False '(' >>= withAttribute "Normal Text") >>~ pushContext "Version2")
226
((pDetectChar False ';' >>= withAttribute "Normal Text") >>~ (popContext >> return ()))
228
((pDetectChar False ')' >>= withAttribute "Normal Text") >>~ (popContext >> return ()))
230
((pRegExpr (compileRegex "[^\\n]+") >>= withAttribute "Error") >>~ (popContext >> return ()))
232
((popContext >> return ()) >> return ([], "")))
233
return (attr, result)
235
parseRules "Version2" =
236
do (attr, result) <- (((pDetectSpaces >>= withAttribute "Version"))
238
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["DigitalMars","X86","AMD64","Windows","Win32","Win64","linux","LittleEndian","BigEndian","D_InlineAsm","none"] >>= withAttribute "Version Type") >>~ (popContext >> return ()))
240
((pDetectIdentifier >>= withAttribute "Normal Text") >>~ (popContext >> return ()))
242
((pInt >>= withAttribute "Integer") >>~ (popContext >> return ()))
244
((pRegExpr (compileRegex "[^);]+") >>= withAttribute "Error") >>~ (popContext >> return ()))
246
((popContext >> return ()) >> return ([], "")))
247
return (attr, result)
249
parseRules "Pragmas" =
250
do (attr, result) <- (((pDetectChar False '(' >>= withAttribute "Normal Text"))
252
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" ["msg","lib"] >>= withAttribute "Version Type") >>~ (popContext >> return ()))
254
((pDetectIdentifier >>= withAttribute "Normal Text") >>~ (popContext >> return ())))
255
return (attr, result)
257
parseRules "RawString" =
258
do (attr, result) <- ((pDetectChar False '"' >>= withAttribute "RawString") >>~ (popContext >> return ()))
259
return (attr, result)
261
parseRules "BQString" =
262
do (attr, result) <- ((pDetectChar False '`' >>= withAttribute "BQString") >>~ (popContext >> return ()))
263
return (attr, result)
265
parseRules "HexString" =
266
do (attr, result) <- (((pDetectChar False '"' >>= withAttribute "HexString") >>~ (popContext >> return ()))
268
((pRegExpr (compileRegex "[^\\sa-fA-F\\d\"]+") >>= withAttribute "Error")))
269
return (attr, result)
271
parseRules "CharLiteral" =
272
do (attr, result) <- (((pHlCStringChar >>= withAttribute "EscapeSequence"))
274
((pDetectChar False '\'' >>= withAttribute "Char") >>~ (popContext >> return ()))
276
((pRegExpr (compileRegex "\\\\(u[\\da-fA-F]{4}|U[\\da-fA-F]{8}|&[a-zA-Z]\\w+;)") >>= withAttribute "EscapeSequence"))
278
((pDetectChar False '\'' >>= withAttribute "Char") >>~ (popContext >> return ()))
280
((pRegExpr (compileRegex ".'") >>= withAttribute "Char"))
282
((popContext >> return ()) >> return ([], "")))
283
return (attr, result)
285
parseRules "String" =
286
do (attr, result) <- (((pDetect2Chars False '\\' '"' >>= withAttribute "String"))
288
((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext >> return ()))
290
((pHlCStringChar >>= withAttribute "EscapeSequence"))
292
((pRegExpr (compileRegex "\\\\(u[\\da-fA-F]{4}|U[\\da-fA-F]{8}|&[a-zA-Z]\\w+;)") >>= withAttribute "EscapeSequence")))
293
return (attr, result)
295
parseRules "CommentLine" =
298
parseRules "CommentBlock" =
299
do (attr, result) <- ((pDetect2Chars False '*' '/' >>= withAttribute "Comment") >>~ (popContext >> return ()))
300
return (attr, result)
302
parseRules "CommentNested" =
303
do (attr, result) <- (((pDetect2Chars False '/' '+' >>= withAttribute "Comment") >>~ pushContext "CommentNested")
305
((pDetect2Chars False '+' '/' >>= withAttribute "Comment") >>~ (popContext >> return ())))
306
return (attr, result)
308
parseRules x = fail $ "Unknown context" ++ x