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

    module Text.Highlighting.Kate.Syntax.Pascal ( 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
    -- | Full name of language.
    syntaxName :: String
    syntaxName = "Pascal"

    -- | Filename extensions for this language.
    syntaxExtensions :: String
    syntaxExtensions = "*.pp;*.pas;*.p"

    -- | 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 = "Pascal" }
      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 [("Pascal",["Normal"])], synStLanguage = "Pascal", synStCurrentLine = "", synStCharsParsedInLine = 0, synStPrevChar = '\n', synStCaseSensitive = True, synStKeywordCaseSensitive = False, synStCaptures = []}

    parseSourceLine = manyTill parseExpressionInternal pEndLine

    pEndLine = do
      newline <|> (eof >> return '\n')
      context <- currentContext
      case context of
        "Normal" -> return ()
        "String" -> (popContext >> return ())
        "Prep1" -> (popContext >> return ())
        "Prep2" -> (popContext >> return ())
        "Comment1" -> return ()
        "Comment2" -> return ()
        "Comment3" -> (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 = [("Keyword","kw"),("ISO/Delphi Extended","kw"),("Type","dt"),("Number","dv"),("String","st"),("Directive","ot"),("Comment","co"),("Alert","al")]

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

    list_keywords = Set.fromList $ words $ "and array asm case const div do downto else file for function goto if in label mod nil not of operator or packed procedure program record repeat set then to type unit until uses var while with xor at automated break continue dispinterface dispose exit false finalization initialization library new published resourcestring self true"
    list_ISO'2fDelphi_Extended = Set.fromList $ words $ "abstract as bindable constructor destructor except export finally import implementation inherited inline interface is module on only otherwise override private property protected public read qualified raise restricted shl shr threadvar try virtual write"
    list_types = Set.fromList $ words $ "integer cardinal shortint smallint longint int64 byte word longword char ansichar widechar boolean bytebool wordbool longbool single double extended comp currency real real48 string shortstring ansistring widestring pointer variant file text"
    list_attention = Set.fromList $ words $ "fixme todo ###"

    regex_'5cb'28begin'7ccase'7crecord'29'28'3f'3d'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29 = compileRegex "\\b(begin|case|record)(?=(\\{[^}]*(\\}|$)|\\(\\*.*(\\*\\)|$))*([\\s]|$|//))"
    regex_'5cb'28'28object'7cclass'29'28'3f'3d'28'5c'28'2e'2a'5c'29'29'3f'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'3b'3f'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29'7ctry'28'3f'3d'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29'29 = compileRegex "\\b((object|class)(?=(\\(.*\\))?(\\{[^}]*(\\}|$)|\\(\\*.*(\\*\\)|$))*;?([\\s]|$|//))|try(?=(\\{[^}]*(\\}|$)|\\(\\*.*(\\*\\)|$))*([\\s]|$|//)))"
    regex_'5cbend'28'3f'3d'28'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'29'28'5b'2e'3b'5cs'5d'7c'24'29'7c'2f'2f'7c'24'29 = compileRegex "\\bend(?=((\\{[^}]*(\\}|$)|\\(\\*.*(\\*\\)|$))*)([.;\\s]|$)|//|$)"

    defaultAttributes = [("Normal","Normal Text"),("String","String"),("Prep1","Directive"),("Prep2","Directive"),("Comment1","Comment"),("Comment2","Comment"),("Comment3","Comment")]

    parseRules "Normal" =
      do (attr, result) <- (((pRegExpr regex_'5cb'28begin'7ccase'7crecord'29'28'3f'3d'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29 >>= withAttribute "Keyword"))
                            <|>
                            ((pRegExpr regex_'5cb'28'28object'7cclass'29'28'3f'3d'28'5c'28'2e'2a'5c'29'29'3f'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'3b'3f'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29'7ctry'28'3f'3d'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'28'5b'5cs'5d'7c'24'7c'2f'2f'29'29'29 >>= withAttribute "ISO/Delphi Extended"))
                            <|>
                            ((pRegExpr regex_'5cbend'28'3f'3d'28'28'5c'7b'5b'5e'7d'5d'2a'28'5c'7d'7c'24'29'7c'5c'28'5c'2a'2e'2a'28'5c'2a'5c'29'7c'24'29'29'2a'29'28'5b'2e'3b'5cs'5d'7c'24'29'7c'2f'2f'7c'24'29 >>= withAttribute "Keyword"))
                            <|>
                            ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword"))
                            <|>
                            ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_ISO'2fDelphi_Extended >>= withAttribute "ISO/Delphi Extended"))
                            <|>
                            ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_types >>= withAttribute "Type"))
                            <|>
                            ((pFloat >>= withAttribute "Number"))
                            <|>
                            ((pInt >>= withAttribute "Number"))
                            <|>
                            ((pDetectChar False '\'' >>= withAttribute "String") >>~ pushContext "String")
                            <|>
                            ((pString False "(*$" >>= withAttribute "Directive") >>~ pushContext "Prep1")
                            <|>
                            ((pDetect2Chars False '{' '$' >>= withAttribute "Directive") >>~ pushContext "Prep2")
                            <|>
                            ((pDetectChar False '{' >>= withAttribute "Comment") >>~ pushContext "Comment1")
                            <|>
                            ((pDetect2Chars False '(' '*' >>= withAttribute "Comment") >>~ pushContext "Comment2")
                            <|>
                            ((pDetect2Chars False '/' '/' >>= withAttribute "Comment") >>~ pushContext "Comment3"))
         return (attr, result)

    parseRules "String" =
      do (attr, result) <- ((pDetectChar False '\'' >>= withAttribute "String") >>~ (popContext >> return ()))
         return (attr, result)

    parseRules "Prep1" =
      do (attr, result) <- ((pDetect2Chars False '*' ')' >>= withAttribute "Directive") >>~ (popContext >> return ()))
         return (attr, result)

    parseRules "Prep2" =
      do (attr, result) <- ((pDetectChar False '}' >>= withAttribute "Directive") >>~ (popContext >> return ()))
         return (attr, result)

    parseRules "Comment1" =
      do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_attention >>= withAttribute "Alert"))
                            <|>
                            ((pDetectChar False '}' >>= withAttribute "Comment") >>~ (popContext >> return ())))
         return (attr, result)

    parseRules "Comment2" =
      do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_attention >>= withAttribute "Alert"))
                            <|>
                            ((pDetect2Chars False '*' ')' >>= withAttribute "Comment") >>~ (popContext >> return ())))
         return (attr, result)

    parseRules "Comment3" =
      do (attr, result) <- ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_attention >>= withAttribute "Alert"))
         return (attr, result)

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