module Text.Highlighting.Kate.Syntax.Ini
(highlight, parseExpression, syntaxName, syntaxExtensions)
where
import Text.Highlighting.Kate.Types
import Text.Highlighting.Kate.Common
import qualified Text.Highlighting.Kate.Syntax.Alert
import Text.ParserCombinators.Parsec hiding (State)
import Control.Monad.State
import Data.Char (isSpace)
import Data.Maybe (fromMaybe)
import qualified Data.Set as Set
syntaxName :: String
syntaxName = "INI Files"
syntaxExtensions :: String
syntaxExtensions = "*.ini;*.pls;*.kcfgc"
highlight :: String -> [SourceLine]
highlight input = evalState (mapM parseSourceLine $ lines input) startingState
parseSourceLine :: String -> State SyntaxState SourceLine
parseSourceLine = mkParseSourceLine parseExpression
parseExpression :: KateParser Token
parseExpression = do
(lang,cont) <- currentContext
let defAttr = fromMaybe NormalTok $ lookup (lang,cont) defaultAttributes
result <- if lang == "INI Files"
then parseRules (lang,cont) <|>
(pDefault >>= withAttribute defAttr)
else parseRules ("INI Files","ini")
optional $ do eof
updateState $ \st -> st{ synStPrevChar = '\n' }
pEndLine
return result
startingState = SyntaxState {synStContexts = [("INI Files","ini")], synStLineNumber = 0, synStPrevChar = '\n', synStPrevNonspace = False, synStCaseSensitive = True, synStKeywordCaseSensitive = False, synStCaptures = []}
pEndLine = do
updateState $ \st -> st{ synStPrevNonspace = False }
context <- currentContext
case context of
("INI Files","ini") -> return ()
("INI Files","Value") -> (popContext) >> pEndLine
("INI Files","Comment") -> (popContext) >> pEndLine
_ -> return ()
withAttribute attr txt = do
when (null txt) $ fail "Parser matched no text"
updateState $ \st -> st { synStPrevChar = last txt
, synStPrevNonspace = synStPrevNonspace st || not (all isSpace txt) }
return (attr, txt)
list_keywords = Set.fromList $ words $ "on off default defaults localhost null true false yes no normal e_all e_error e_warning e_parse e_notice e_strict e_core_error e_core_warning e_compile_error e_compile_warning e_user_error e_user_warning e_user_notice"
regex_'3b'2e'2a'24 = compileRegex ";.*$"
regex_'23'2e'2a'24 = compileRegex "#.*$"
defaultAttributes = [(("INI Files","ini"),DataTypeTok),(("INI Files","Value"),StringTok),(("INI Files","Comment"),CommentTok)]
parseRules ("INI Files","ini") =
(((pRangeDetect '[' ']' >>= withAttribute KeywordTok) >>~ (popContext))
<|>
((pDetectChar False '=' >>= withAttribute OtherTok) >>~ pushContext ("INI Files","Value"))
<|>
((pFirstNonSpace >> pDetectChar False ';' >>= withAttribute CommentTok) >>~ pushContext ("INI Files","Comment"))
<|>
((pFirstNonSpace >> pDetectChar False '#' >>= withAttribute CommentTok) >>~ pushContext ("INI Files","Comment")))
parseRules ("INI Files","Value") =
(((pFloat >>= withAttribute FloatTok))
<|>
((pInt >>= withAttribute DecValTok))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute KeywordTok))
<|>
((pRegExpr regex_'3b'2e'2a'24 >>= withAttribute CommentTok) >>~ (popContext))
<|>
((pRegExpr regex_'23'2e'2a'24 >>= withAttribute CommentTok) >>~ (popContext)))
parseRules ("INI Files","Comment") =
(((pDetectSpaces >>= withAttribute CommentTok))
<|>
((Text.Highlighting.Kate.Syntax.Alert.parseExpression >>= ((withAttribute CommentTok) . snd)))
<|>
((pDetectIdentifier >>= withAttribute CommentTok)))
parseRules ("Alerts", _) = Text.Highlighting.Kate.Syntax.Alert.parseExpression
parseRules x = fail $ "Unknown context" ++ show x