    {- This module was generated from data in the Kate syntax highlighting file cmake.xml, version 1.04,
       by   -}

    module Text.Highlighting.Kate.Syntax.Cmake ( 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 = "CMake"

    -- | Filename extensions for this language.
    syntaxExtensions :: String
    syntaxExtensions = "CMakeLists.txt;*.cmake;"

    -- | 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 = "CMake" }
      context <- currentContext <|> (pushContext "Normal Text" >> 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 [("CMake",["Normal Text"])], synStLanguage = "CMake", 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 Text" -> return ()
        "Function Args" -> return ()
        "Comment" -> (popContext >> return ())
        _ -> return ()
      lineContents <- lookAhead wholeLine
      updateState $ \st -> st { synStCurrentLine = lineContents, synStCharsParsedInLine = 0, synStPrevChar = '\n' }

    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 = [("Special Args","ot"),("Commands","kw"),("Macros","kw"),("Variable","dv"),("Comment","co"),("Region Marker","re")]

    parseExpressionInternal = do
      context <- currentContext
      parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes))

    list_commands = Set.fromList $ words $ "ABSTRACT_FILES ADD_CUSTOM_COMMAND ADD_CUSTOM_TARGET ADD_DEFINITIONS ADD_DEPENDENCIES ADD_EXECUTABLE ADD_LIBRARY ADD_SUBDIRECTORY ADD_TEST AUX_SOURCE_DIRECTORY BUILD_COMMAND BUILD_NAME CMAKE_MINIMUM_REQUIRED CONFIGURE_FILE CREATE_TEST_SOURCELIST ELSE ELSEIF ENABLE_TESTING ENDFOREACH ENDIF ENDMACRO EXEC_PROGRAM EXPORT_LIBRARY_DEPENDENCIES FILE FIND_FILE FIND_LIBRARY FIND_PACKAGE FIND_PATH FIND_PROGRAM FLTK_WRAP_UI FOREACH GET_CMAKE_PROPERTY GET_DIRECTORY_PROPERTY GET_FILENAME_COMPONENT GET_SOURCE_FILE_PROPERTY GET_TARGET_PROPERTY IF INCLUDE INCLUDE_DIRECTORIES INCLUDE_EXTERNAL_MSPROJECT INCLUDE_REGULAR_EXPRESSION INSTALL INSTALL_FILES INSTALL_PROGRAMS INSTALL_TARGETS ITK_WRAP_TCL LINK_DIRECTORIES LINK_LIBRARIES LIST LOAD_CACHE LOAD_COMMAND MACRO MAKE_DIRECTORY MARK_AS_ADVANCED MESSAGE OPTION OUTPUT_REQUIRED_FILES PROJECT QT_WRAP_CPP QT_WRAP_UI REMOVE REMOVE_DEFINITIONS SEPARATE_ARGUMENTS SET SET_DIRECTORY_PROPERTIES SET_SOURCE_FILES_PROPERTIES SET_TARGET_PROPERTIES SITE_NAME SOURCE_FILES SOURCE_FILES_REMOVE SOURCE_GROUP STRING SUBDIRS SUBDIR_DEPENDS TARGET_LINK_LIBRARIES TRY_COMPILE TRY_RUN USE_MANGLED_MESA UTILITY_SOURCE VARIABLE_REQUIRES VTK_MAKE_INSTANTIATOR VTK_WRAP_JAVA VTK_WRAP_PYTHON VTK_WRAP_TCL WRAP_EXCLUDE_FILES WRITE_FILE"
    list_special'5fargs = Set.fromList $ words $ "ABSOLUTE ABSTRACT ADDITIONAL_MAKE_CLEAN_FILES ALL AND APPEND ARCHIVE ARGS ASCII BEFORE CACHE CACHE_VARIABLES CLEAR CMAKE_FLAGS CODE COMMAND COMMANDS COMMAND_NAME COMMENT COMPARE COMPILE_FLAGS COMPONENT CONFIGURATIONS COPYONLY DEFINED DEFINE_SYMBOL DEPENDS DESTINATION DIRECTORY_PERMISSIONS DOC EQUAL ESCAPE_QUOTES EXCLUDE EXCLUDE_FROM_ALL EXISTS EXPORT_MACRO EXT EXTRA_INCLUDE FATAL_ERROR FILE FILES FILE_PERMISSIONS FORCE FUNCTION GENERATED GLOB GLOB_RECURSE GREATER GROUP_SIZE HEADER_FILE_ONLY HEADER_LOCATION IMMEDIATE INCLUDES INCLUDE_DIRECTORIES INCLUDE_INTERNALS INCLUDE_REGULAR_EXPRESSION INTERNAL LESS LIBRARY LINK_DIRECTORIES LINK_FLAGS LOCATION MACOSX_BUNDLE MACROS MAIN_DEPENDENCY MAKE_DIRECTORY MATCH MATCHALL MATCHES MODULE NAME NAMES NAME_WE NOT NOTEQUAL NO_SYSTEM_PATH OBJECT_DEPENDS OPTIONAL OR OUTPUT OUTPUT_VARIABLE PATH PATHS PATTERN PERMISSIONS POST_BUILD POST_INSTALL_SCRIPT PREFIX PREORDER PRE_BUILD PRE_INSTALL_SCRIPT PRE_LINK PROGRAM PROGRAMS PROGRAM_ARGS PROPERTIES QUIET RANGE READ REGEX REGULAR_EXPRESSION RENAME REPLACE REQUIRED RETURN_VALUE RUNTIME RUNTIME_DIRECTORY SCRIPT SEND_ERROR SHARED SOURCES STATIC STATUS STREQUAL STRGREATER STRLESS SUFFIX TARGET TARGETS TOLOWER TOUPPER USE_SOURCE_PERMISSIONS VAR VARIABLES VERSION WIN32 WRAP_EXCLUDE WRITE"

    regex_'23'5cs'2aBEGIN'2e'2a'24 = compileRegex "#\\s*BEGIN.*$"
    regex_'23'5cs'2aEND'2e'2a'24 = compileRegex "#\\s*END.*$"
    regex_'5c'24'5c'7b'5cs'2a'5cw'2b'5cs'2a'5c'7d = compileRegex "\\$\\{\\s*\\w+\\s*\\}"
    regex_'5cw'2b'5cs'2a'28'3f'3d'5c'28'29 = compileRegex "\\w+\\s*(?=\\()"
    regex_'23'2e'2a'24 = compileRegex "#.*$"

    defaultAttributes = [("Normal Text","Normal Text"),("Function Args","Normal Text"),("Comment","Comment")]

    parseRules "Normal Text" =
      do (attr, result) <- (((pDetectSpaces >>= withAttribute "Normal Text"))
                            <|>
                            ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_commands >>= withAttribute "Commands") >>~ pushContext "Function Args")
                            <|>
                            ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2aBEGIN'2e'2a'24 >>= withAttribute "Region Marker"))
                            <|>
                            ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2aEND'2e'2a'24 >>= withAttribute "Region Marker"))
                            <|>
                            ((pDetectChar False '#' >>= withAttribute "Comment") >>~ pushContext "Comment")
                            <|>
                            ((pRegExpr regex_'5c'24'5c'7b'5cs'2a'5cw'2b'5cs'2a'5c'7d >>= withAttribute "Variable"))
                            <|>
                            ((pRegExpr regex_'5cw'2b'5cs'2a'28'3f'3d'5c'28'29 >>= withAttribute "Macros")))
         return (attr, result)

    parseRules "Function Args" =
      do (attr, result) <- (((pDetectChar False ')' >>= withAttribute "Normal Text") >>~ (popContext >> return ()))
                            <|>
                            ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_special'5fargs >>= withAttribute "Special Args"))
                            <|>
                            ((pRegExpr regex_'23'2e'2a'24 >>= withAttribute "Comment"))
                            <|>
                            ((pRegExpr regex_'5c'24'5c'7b'5cs'2a'5cw'2b'5cs'2a'5c'7d >>= withAttribute "Variable")))
         return (attr, result)

    parseRules "Comment" =
      do (attr, result) <- ((Text.Highlighting.Kate.Syntax.Alert.parseExpression))
         return (attr, result)

    parseRules x = fail $ "Unknown context" ++ x