{- This module was generated from data in the Kate syntax highlighting file gnuassembler.xml, version 1.05,
   by  John Zaitseff (J.Zaitseff@zap.org.au), Roland Pabel (roland@pabel.name), Miquel Sabaté (mikisabate@gmail.com) -}

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

-- | Filename extensions for this language.
syntaxExtensions :: String
syntaxExtensions = "*.s;*.S"

-- | 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 = "GNU Assembler" }
  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 [("GNU Assembler",["Normal"])], synStLanguage = "GNU Assembler", 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" -> return () >> pHandleEndLine
    "Commentar 2" -> (popContext) >> pEndLine
    "String" -> (popContext) >> pEndLine
    "Preprocessor" -> (popContext) >> pEndLine
    "Define" -> (popContext) >> pEndLine
    "Some Context" -> (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 = [("Label","kw"),("Keyword","kw"),("Decimal","dv"),("Octal","bn"),("Hex","bn"),("Binary","bn"),("Float","fl"),("Char","ch"),("String","st"),("String Char","ch"),("Comment","co"),("Preprocessor","ot")]

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

list_keywords = Set.fromList $ words $ ".abort .align .app-file .appline .ascii .asciz .att_syntax .balign .balignl .balignw .byte .code16 .code32 .comm .common.s .common .data .dc.b .dc.d .dc.l .dc.s .dc.w .dc.x .dc .dcb.b .dcb.d .dcb.l .dcb.s .dcb.w .dcb.x .dcb .debug .def .desc .dim .double .ds.b .ds.d .ds.l .ds.p .ds.s .ds.w .ds.x .ds .dsect .eject .else .elsec .elseif .end .endc .endef .endfunc .endif .endm .endr .equ .equiv .err .exitm .extend .extern .fail .file .fill .float .format .func .global .globl .hidden .hword .ident .if .ifc .ifdef .ifeq .ifeqs .ifge .ifgt .ifle .iflt .ifnc .ifndef .ifne .ifnes .ifnotdef .include .int .intel_syntax .internal .irep .irepc .irp .irpc .lcomm .lflags .line .linkonce .list .llen .ln .long .lsym .macro .mexit .name .noformat .nolist .nopage noprefix .octa .offset .org .p2align .p2alignl .p2alignw .page .plen .popsection .previous .print .protected .psize .purgem .pushsection .quad .rodata .rep .rept .rva .sbttl .scl .sect.s .sect .section.s .section .set .short .single .size .skip .sleb128 .space .spc .stabd .stabn .stabs .string .struct .subsection .symver .tag .text .title .ttl .type .uleb128 .use .val .version .vtable_entry .vtable_inherit .weak .word .xcom .xdef .xref .xstabs .zero .arm .bss .code .even .force_thumb .ldouble .loc .ltorg .packed .pool .req .thumb .thumb_func .thumb_set"

regex_'5b'5f'5cw'5cd'2d'5d'2a'5cs'2a'3a = compileRegex "[_\\w\\d-]*\\s*:"
regex_0'5bbB'5d'5b01'5d'2b = compileRegex "0[bB][01]+"
regex_0'5bfFeEdD'5d'5b'2d'2b'5d'3f'5b0'2d9'5d'2a'5c'2e'3f'5b0'2d9'5d'2a'5beE'5d'3f'5b'2d'2b'5d'3f'5b0'2d9'5d'2b = compileRegex "0[fFeEdD][-+]?[0-9]*\\.?[0-9]*[eE]?[-+]?[0-9]+"
regex_'5bA'2dZa'2dz'5f'2e'24'5d'5bA'2dZa'2dz0'2d9'5f'2e'24'5d'2a = compileRegex "[A-Za-z_.$][A-Za-z0-9_.$]*"
regex_'27'28'5c'5cx'5b0'2d9a'2dfA'2dF'5d'5b0'2d9a'2dfA'2dF'5d'3f'7c'5c'5c'5b0'2d7'5d'3f'5b0'2d7'5d'3f'5b0'2d7'5d'3f'7c'5c'5c'2e'7c'2e'29 = compileRegex "'(\\\\x[0-9a-fA-F][0-9a-fA-F]?|\\\\[0-7]?[0-7]?[0-7]?|\\\\.|.)"
regex_'23'5cs'2aif'28'3f'3adef'7cndef'29'3f'28'3f'3d'5cs'2b'5cS'29 = compileRegex "#\\s*if(?:def|ndef)?(?=\\s+\\S)"
regex_'23'5cs'2aendif = compileRegex "#\\s*endif"
regex_'23'5cs'2adefine'2e'2a'28'28'3f'3d'5c'5c'29'29 = compileRegex "#\\s*define.*((?=\\\\))"
regex_'23'5cs'2a'28'3f'3ael'28'3f'3ase'7cif'29'7cinclude'28'3f'3a'5fnext'29'3f'7cdefine'7cundef'7cline'7cerror'7cwarning'7cpragma'29 = compileRegex "#\\s*(?:el(?:se|if)|include(?:_next)?|define|undef|line|error|warning|pragma)"

defaultAttributes = [("Normal","Normal Text"),("Commentar 1","Comment"),("Commentar 2","Comment"),("String","String"),("Preprocessor","Preprocessor"),("Define","Preprocessor"),("Some Context","Normal Text")]

parseRules "Normal" = 
  do (attr, result) <- (((pRegExpr regex_'5b'5f'5cw'5cd'2d'5d'2a'5cs'2a'3a >>= withAttribute "Label"))
                        <|>
                        ((pKeyword " \n\t():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword"))
                        <|>
                        ((pHlCOct >>= withAttribute "Octal"))
                        <|>
                        ((pHlCHex >>= withAttribute "Hex"))
                        <|>
                        ((pRegExpr regex_0'5bbB'5d'5b01'5d'2b >>= withAttribute "Binary"))
                        <|>
                        ((pInt >>= withAttribute "Decimal"))
                        <|>
                        ((pRegExpr regex_0'5bfFeEdD'5d'5b'2d'2b'5d'3f'5b0'2d9'5d'2a'5c'2e'3f'5b0'2d9'5d'2a'5beE'5d'3f'5b'2d'2b'5d'3f'5b0'2d9'5d'2b >>= withAttribute "Float"))
                        <|>
                        ((pRegExpr regex_'5bA'2dZa'2dz'5f'2e'24'5d'5bA'2dZa'2dz0'2d9'5f'2e'24'5d'2a >>= withAttribute "Normal Text"))
                        <|>
                        ((pHlCChar >>= withAttribute "Char"))
                        <|>
                        ((pRegExpr regex_'27'28'5c'5cx'5b0'2d9a'2dfA'2dF'5d'5b0'2d9a'2dfA'2dF'5d'3f'7c'5c'5c'5b0'2d7'5d'3f'5b0'2d7'5d'3f'5b0'2d7'5d'3f'7c'5c'5c'2e'7c'2e'29 >>= withAttribute "Char"))
                        <|>
                        ((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")
                        <|>
                        ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2aif'28'3f'3adef'7cndef'29'3f'28'3f'3d'5cs'2b'5cS'29 >>= withAttribute "Preprocessor") >>~ pushContext "Preprocessor")
                        <|>
                        ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2aendif >>= withAttribute "Preprocessor") >>~ pushContext "Preprocessor")
                        <|>
                        ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2adefine'2e'2a'28'28'3f'3d'5c'5c'29'29 >>= withAttribute "Preprocessor") >>~ pushContext "Define")
                        <|>
                        ((pFirstNonSpace >> pRegExpr regex_'23'5cs'2a'28'3f'3ael'28'3f'3ase'7cif'29'7cinclude'28'3f'3a'5fnext'29'3f'7cdefine'7cundef'7cline'7cerror'7cwarning'7cpragma'29 >>= withAttribute "Preprocessor") >>~ pushContext "Preprocessor")
                        <|>
                        ((pDetect2Chars False '/' '*' >>= withAttribute "Comment") >>~ pushContext "Commentar 1")
                        <|>
                        ((pAnyChar "@;#" >>= withAttribute "Comment") >>~ pushContext "Commentar 2")
                        <|>
                        ((pAnyChar "!#%&*()+,-<=>?/:[]^{|}~" >>= withAttribute "Symbol")))
     return (attr, result)

parseRules "Commentar 1" = 
  do (attr, result) <- ((pDetect2Chars False '*' '/' >>= withAttribute "Comment") >>~ (popContext))
     return (attr, result)

parseRules "Commentar 2" = 
  pzero

parseRules "String" = 
  do (attr, result) <- (((pLineContinue >>= withAttribute "String") >>~ pushContext "Some Context")
                        <|>
                        ((pHlCStringChar >>= withAttribute "String Char"))
                        <|>
                        ((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext)))
     return (attr, result)

parseRules "Preprocessor" = 
  pzero

parseRules "Define" = 
  do (attr, result) <- ((pLineContinue >>= withAttribute "Preprocessor"))
     return (attr, result)

parseRules "Some Context" = 
  pzero

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