module Text.Highlighting.Kate.Syntax.Cue ( highlight, parseExpression, syntaxName, syntaxExtensions ) where
import Text.Highlighting.Kate.Definitions
import Text.Highlighting.Kate.Common
import Text.ParserCombinators.Parsec
import Control.Monad (when)
import Data.Map (fromList)
import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Set as Set
syntaxName :: String
syntaxName = "CUE Sheet"
syntaxExtensions :: String
syntaxExtensions = "*.cue"
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 = "CUE Sheet" }
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 [("CUE Sheet",["Normal"])], synStLanguage = "CUE Sheet", synStCurrentLine = "", synStCharsParsedInLine = 0, synStPrevChar = '\n', synStCaseSensitive = True, synStKeywordCaseSensitive = False, synStCaptures = []}
parseSourceLine = manyTill parseExpressionInternal pEndLine
pEndLine = do
lookAhead $ newline <|> (eof >> return '\n')
context <- currentContext
case context of
"Normal" -> return () >> pHandleEndLine
"String" -> (popContext) >> pEndLine
"Comment" -> (popContext) >> pEndLine
_ -> pHandleEndLine
withAttribute attr txt = do
when (null txt) $ fail "Parser matched no text"
let labs = attr : maybeToList (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 (labs, txt)
styles = [("Keyword","kw"),("Decimal","dv"),("String","st"),("Comment","co")]
parseExpressionInternal = do
context <- currentContext
parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes))
list_keywords = Set.fromList $ words $ "catalog cdtextfile file flags index isrc performer pregap postgap rem songwriter title track"
list_format = Set.fromList $ words $ "aiff wave mp3 binary mototola"
list_mode = Set.fromList $ words $ "audio cdg cdi mode1 mode2 raw"
list_flags = Set.fromList $ words $ "4ch dcp pre scms"
defaultAttributes = [("Normal","Normal Text"),("String","String"),("Comment","Comment")]
parseRules "Normal" =
do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword"))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_format >>= withAttribute "Format"))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_mode >>= withAttribute "Mode"))
<|>
((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_flags >>= withAttribute "Flags"))
<|>
((pInt >>= withAttribute "Decimal"))
<|>
((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")
<|>
((pDetectChar False ';' >>= withAttribute "Comment") >>~ pushContext "Comment"))
return (attr, result)
parseRules "String" =
do (attr, result) <- (((pLineContinue >>= withAttribute "String"))
<|>
((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext)))
return (attr, result)
parseRules "Comment" =
pzero
parseRules x = fail $ "Unknown context" ++ x