module Text.Highlighting.Kate.Syntax.Ocaml ( highlight, parseExpression, syntaxName, syntaxExtensions ) where
import Text.Highlighting.Kate.Definitions
import Text.Highlighting.Kate.Common
import Text.ParserCombinators.Parsec
import Data.List (nub)
import qualified Data.Set as Set
import Data.Map (fromList)
import Data.Maybe (fromMaybe)
syntaxName :: String
syntaxName = "Objective Caml"
syntaxExtensions :: String
syntaxExtensions = "*.ml;*.mli"
highlight :: String -> Either String [SourceLine]
highlight input =
case runParser parseSource startingState "source" input of
Left err -> Left $ show err
Right result -> Right result
parseExpression :: GenParser Char SyntaxState LabeledSource
parseExpression = do
st <- getState
let oldLang = synStLanguage st
setState $ st { synStLanguage = "Objective Caml" }
context <- currentContext <|> (pushContext "Normal" >> currentContext)
result <- parseRules context
updateState $ \st -> st { synStLanguage = oldLang }
return result
parseSource = do
lineContents <- lookAhead wholeLine
updateState $ \st -> st { synStCurrentLine = lineContents }
result <- manyTill parseSourceLine eof
return $ map normalizeHighlighting result
startingState = SyntaxState {synStContexts = fromList [("Objective Caml",["Normal"])], synStLanguage = "Objective Caml", synStCurrentLine = "", synStCharsParsedInLine = 0, synStPrevChar = '\n', synStCaseSensitive = True, synStKeywordCaseSensitive = True, synStCaptures = []}
parseSourceLine = manyTill parseExpressionInternal pEndLine
pEndLine = do
newline <|> (eof >> return '\n')
context <- currentContext
case context of
"Normal" -> return ()
"Multiline Comment" -> return ()
"String Constant" -> return ()
"Camlp4 Quotation Constant" -> return ()
_ -> return ()
lineContents <- lookAhead wholeLine
updateState $ \st -> st { synStCurrentLine = lineContents, synStCharsParsedInLine = 0, synStPrevChar = '\n' }
withAttribute attr txt = do
if null txt
then fail "Parser matched no text"
else return ()
let style = fromMaybe "" $ lookup attr styles
st <- getState
let oldCharsParsed = synStCharsParsedInLine st
let prevchar = if null txt then '\n' else last txt
updateState $ \st -> st { synStCharsParsedInLine = oldCharsParsed + length txt, synStPrevChar = prevchar }
return (nub [style, attr], txt)
styles = [("Normal Text","Normal"),("Identifier","Normal"),("Keyword","Keyword"),("Revised Syntax Keyword","Normal"),("Core Data Type","DataType"),("Decimal","DecVal"),("Hexadecimal","BaseN"),("Octal","BaseN"),("Binary","BaseN"),("Float","Float"),("Character","Char"),("String","String"),("Escaped characters","Char"),("Comment","Comment"),("Camlp4 Quotation","String"),("Directive","Others")]
parseExpressionInternal = do
context <- currentContext
parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes))
list980ceefe02086ad13446778462ab2aa6507289e1 = Set.fromList $ words $ "declare value where"
list35ec2c7ccd5c5bffb9190d6de745f2dbb365db85 = Set.fromList $ words $ "and as assert asr begin class closed constraint do done downto else end exception external false for fun function functor if in include inherit land lazy let lor lsl lsr lxor match method mod module mutable new of open or parser private rec sig struct then to true try type val virtual when while with"
list129e98f14d82c802c33db89f9d8007f6e565c085 = Set.fromList $ words $ "exn lazy_t format unit int real char string ref array bool list option"
defaultAttributes = [("Normal","Normal Text"),("Multiline Comment","Comment"),("String Constant","String"),("Camlp4 Quotation Constant","Camlp4 Quotation")]
parseRules "Normal" =
do (attr, result) <- (((pDetect2Chars False '(' '*' >>= withAttribute "Comment") >>~ pushContext "Multiline Comment")
<|>
((pFirstNonSpace >> pRegExpr (compileRegex "#[A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\0377_][A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\03770-9_']*.*$") >>= withAttribute "Directive"))
<|>
((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String Constant")
<|>
((pRegExpr (compileRegex "'((\\\\[ntbr'\"\\\\]|\\\\[0-9]{3}|\\\\x[0-9A-Fa-f]{2})|[^'])'") >>= withAttribute "Character"))
<|>
((pDetect2Chars False '<' '<' >>= withAttribute "Camlp4 Quotation") >>~ pushContext "Camlp4 Quotation Constant")
<|>
((pRegExpr (compileRegex "<:[A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\0377_][A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\03770-9_']*<") >>= withAttribute "Camlp4 Quotation") >>~ pushContext "Camlp4 Quotation Constant")
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list35ec2c7ccd5c5bffb9190d6de745f2dbb365db85 >>= withAttribute "Keyword"))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list980ceefe02086ad13446778462ab2aa6507289e1 >>= withAttribute "Revised Syntax Keyword"))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list129e98f14d82c802c33db89f9d8007f6e565c085 >>= withAttribute "Core Data Type"))
<|>
((pRegExpr (compileRegex "[A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\0377_][A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\03770-9_']*") >>= withAttribute "Identifier"))
<|>
((pRegExpr (compileRegex "-?0[xX][0-9A-Fa-f_]+") >>= withAttribute "Hexadecimal"))
<|>
((pRegExpr (compileRegex "-?0[oO][0-7_]+") >>= withAttribute "Octal"))
<|>
((pRegExpr (compileRegex "-?0[bB][01_]+") >>= withAttribute "Binary"))
<|>
((pRegExpr (compileRegex "-?[0-9][0-9_]*(\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?|[eE][-+]?[0-9][0-9_]*)") >>= withAttribute "Float"))
<|>
((pRegExpr (compileRegex "-?[0-9][0-9_]*") >>= withAttribute "Decimal")))
return (attr, result)
parseRules "Multiline Comment" =
do (attr, result) <- (((pDetect2Chars False '*' ')' >>= withAttribute "Comment") >>~ (popContext >> return ()))
<|>
((pDetect2Chars False '(' '*' >>= withAttribute "Comment") >>~ pushContext "Multiline Comment"))
return (attr, result)
parseRules "String Constant" =
do (attr, result) <- (((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext >> return ()))
<|>
((pRegExpr (compileRegex "(\\\\[ntbr'\"\\\\]|\\\\[0-9]{3}|\\\\x[0-9A-Fa-f]{2})") >>= withAttribute "Escaped characters"))
<|>
((pRegExpr (compileRegex "\\\\$") >>= withAttribute "Escaped characters")))
return (attr, result)
parseRules "Camlp4 Quotation Constant" =
do (attr, result) <- (((pDetect2Chars False '>' '>' >>= withAttribute "Camlp4 Quotation") >>~ (popContext >> return ()))
<|>
((pDetect2Chars False '<' '<' >>= withAttribute "Camlp4 Quotation") >>~ pushContext "Camlp4 Quotation Constant")
<|>
((pRegExpr (compileRegex "<:[A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\0377_][A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\03770-9_']*<") >>= withAttribute "Camlp4 Quotation") >>~ pushContext "Camlp4 Quotation Constant")
<|>
((pRegExpr (compileRegex "\\\\(\\\\|>>|<<)") >>= withAttribute "Escaped characters"))
<|>
((pRegExpr (compileRegex "\\\\<:[A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\0377_][A-Za-z\\0300-\\0326\\0330-\\0366\\0370-\\03770-9_']*<") >>= withAttribute "Escaped characters")))
return (attr, result)
parseRules x = fail $ "Unknown context" ++ x