{- This module was generated from data in the Kate syntax highlighting file go.xml, version 1.00, by Miquel Sabaté (mikisabate@gmail.com) -} module Text.Highlighting.Kate.Syntax.Go ( highlight, parseExpression, syntaxName, syntaxExtensions ) where import Text.Highlighting.Kate.Definitions import Text.Highlighting.Kate.Common import qualified Text.Highlighting.Kate.Syntax.Alert import Text.ParserCombinators.Parsec import Control.Monad (when) import Data.Map (fromList) import Data.Maybe (fromMaybe, maybeToList) import qualified Data.Set as Set -- | Full name of language. syntaxName :: String syntaxName = "Go" -- | Filename extensions for this language. syntaxExtensions :: String syntaxExtensions = "*.go" -- | Highlight source code using this syntax definition. highlight :: String -> Either String [SourceLine] highlight input = case runParser parseSource startingState "source" input of Left err -> Left $ show err Right result -> Right result -- | Parse an expression using appropriate local context. parseExpression :: GenParser Char SyntaxState LabeledSource parseExpression = do st <- getState let oldLang = synStLanguage st setState $ st { synStLanguage = "Go" } 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 [("Go",["normal"])], synStLanguage = "Go", synStCurrentLine = "", synStCharsParsedInLine = 0, synStPrevChar = '\n', synStCaseSensitive = True, synStKeywordCaseSensitive = True, synStCaptures = []} parseSourceLine = manyTill parseExpressionInternal pEndLine pEndLine = do lookAhead $ newline <|> (eof >> return '\n') context <- currentContext case context of "normal" -> return () >> pHandleEndLine "Commentar 1" -> (popContext) >> pEndLine "Commentar 2" -> return () >> pHandleEndLine "Package" -> (popContext) >> pEndLine "String" -> (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"),("Special","ot"),("Type","dt"),("Builtin","ot"),("String Char","ch"),("String","st"),("Comment","co"),("Char","ch")] parseExpressionInternal = do context <- currentContext parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes)) list_keywords = Set.fromList $ words $ "break case chan const continue default defer else fallthrough for func go goto if import interface map range return select struct switch type var" list_types = Set.fromList $ words $ "bool byte complex64 complex128 float float32 float64 int int8 int16 int32 int64 string uint uint8 uint16 uint32 uint64" list_builtin = Set.fromList $ words $ "cap len make new panic recover" list_special = Set.fromList $ words $ "false nil true" defaultAttributes = [("normal","Normal Text"),("Commentar 1","Comment"),("Commentar 2","Comment"),("Package","Package"),("String","String")] parseRules "normal" = do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword")) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_special >>= withAttribute "Special")) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_types >>= withAttribute "Type")) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_builtin >>= withAttribute "Builtin")) <|> ((pString False "package" >>= withAttribute "Package") >>~ pushContext "Package") <|> ((pDetectChar False '{' >>= withAttribute "Symbol")) <|> ((pDetectChar False '}' >>= withAttribute "Symbol")) <|> ((pDetect2Chars False '/' '/' >>= withAttribute "Comment") >>~ pushContext "Commentar 1") <|> ((pDetect2Chars False '/' '*' >>= withAttribute "Comment") >>~ pushContext "Commentar 2") <|> ((pHlCChar >>= withAttribute "Char")) <|> ((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")) return (attr, result) parseRules "Commentar 1" = do (attr, result) <- (((pLineContinue >>= withAttribute "Comment")) <|> ((pDetectSpaces >>= withAttribute "Comment")) <|> ((Text.Highlighting.Kate.Syntax.Alert.parseExpression)) <|> ((pDetectIdentifier >>= withAttribute "Comment"))) return (attr, result) parseRules "Commentar 2" = do (attr, result) <- (((pDetectSpaces >>= withAttribute "Comment")) <|> ((pDetect2Chars False '*' '/' >>= withAttribute "Comment") >>~ (popContext)) <|> ((Text.Highlighting.Kate.Syntax.Alert.parseExpression)) <|> ((pDetectIdentifier >>= withAttribute "Comment"))) return (attr, result) parseRules "Package" = pzero parseRules "String" = do (attr, result) <- (((pLineContinue >>= withAttribute "String")) <|> ((pHlCStringChar >>= withAttribute "String Char")) <|> ((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext))) return (attr, result) parseRules "" = parseRules "normal" parseRules x = fail $ "Unknown context" ++ x