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

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

-- | Filename extensions for this language.
syntaxExtensions :: String
syntaxExtensions = "*.adb;*.ads;*.ada;*.a"

-- | 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 = "Ada" }
  context <- currentContext <|> (pushContext "Default" >> 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 [("Ada",["Default"])], synStLanguage = "Ada", 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
    "Default" -> return () >> pHandleEndLine
    "Region Marker" -> (popContext) >> pEndLine
    "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"),("Pragmas","kw"),("Data Type","dt"),("Decimal","dv"),("Base-N","bn"),("Float","fl"),("Char","ch"),("String","st"),("Comment","co"),("Region Marker","re")]

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

list_keywords = Set.fromList $ words $ "abort abs abstract accept access aliased all and array at begin body constant declare delay delta digits do else elsif end entry exception exit for function generic goto in interface is limited mod new not null of or others out overriding package pragma private procedure protected raise range rem record renames requeue return reverse separate subtype tagged task terminate then type until use when while with xor"
list_pragmas = Set.fromList $ words $ "all_calls_remote assert assertion_policy asynchronous atomic atomic_components attach_handler controlled convention detect_blocking discard_names elaborate elaborate_all elaborate_body export import inline inspection_point interrupt_handler interrupt_priority linker_options list locking_policy no_return normalize_scalars optimize pack page partition_elaboration_policy preelaborable_initialization preelaborate priority priority_specific_dispatching profile pure queuing_policy relative_deadline remote_call_interface remote_types restrictions reviewable shared_passive storage_size suppress task_dispatching_policy unchecked_union unsuppress volatile volatile_components"
list_types = Set.fromList $ words $ "boolean char float integer long_float long_integer long_long_float long_long_integer short_float short_integer string wide_string wide_char wide_wide_char wide_wide_string"

regex_'5cbrecord'5cb = compileRegex "\\brecord\\b"
regex_'5cbend'5cs'2brecord'5cb = compileRegex "\\bend\\s+record\\b"
regex_'5cbcase'5cb = compileRegex "\\bcase\\b"
regex_'5cbend'5cs'2bcase'5cb = compileRegex "\\bend\\s+case\\b"
regex_'5cbif'5cb = compileRegex "\\bif\\b"
regex_'5cbend'5cs'2bif'5cb = compileRegex "\\bend\\s+if\\b"
regex_'5cbloop'5cb = compileRegex "\\bloop\\b"
regex_'5cbend'5cs'2bloop'5cb = compileRegex "\\bend\\s+loop\\b"
regex_'5cbselect'5cb = compileRegex "\\bselect\\b"
regex_'5cbend'5cs'2bselect'5cb = compileRegex "\\bend\\s+select\\b"
regex_'5cbbegin'5cb = compileRegex "\\bbegin\\b"
regex_'5cbend'5cb = compileRegex "\\bend\\b"
regex_'27'2e'27 = compileRegex "'.'"

defaultAttributes = [("Default","Normal Text"),("Region Marker","Region Marker"),("String","String"),("Comment","Comment")]

parseRules "Default" = 
  do (attr, result) <- (((pRegExpr regex_'5cbrecord'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cs'2brecord'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbcase'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cs'2bcase'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbif'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cs'2bif'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbloop'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cs'2bloop'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbselect'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cs'2bselect'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbbegin'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pRegExpr regex_'5cbend'5cb >>= withAttribute "Keyword"))
                        <|>
                        ((pFirstNonSpace >> pString False "--  BEGIN" >>= withAttribute "Region Marker") >>~ pushContext "Region Marker")
                        <|>
                        ((pFirstNonSpace >> pString False "--  END" >>= withAttribute "Region Marker") >>~ pushContext "Region Marker")
                        <|>
                        ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword"))
                        <|>
                        ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_pragmas >>= withAttribute "Pragmas"))
                        <|>
                        ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_types >>= withAttribute "Data Type"))
                        <|>
                        ((pFloat >>= withAttribute "Float"))
                        <|>
                        ((pInt >>= withAttribute "Decimal"))
                        <|>
                        ((pRegExpr regex_'27'2e'27 >>= withAttribute "Char"))
                        <|>
                        ((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")
                        <|>
                        ((pDetect2Chars False '-' '-' >>= withAttribute "Comment") >>~ pushContext "Comment")
                        <|>
                        ((pAnyChar ":!%&()+,-/.*<=>|" >>= withAttribute "Symbol")))
     return (attr, result)

parseRules "Region Marker" = 
  pzero

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

parseRules "Comment" = 
  pzero

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