{- This module was generated from data in the Kate syntax highlighting file asn1.xml, version 1.00, by Philippe Rigault -} module Text.Highlighting.Kate.Syntax.Asn1 ( 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 = "ASN.1" -- | Filename extensions for this language. syntaxExtensions :: String syntaxExtensions = "*.asn*.asn1" -- | 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 = "ASN.1" } 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 [("ASN.1",["Normal Text"])], synStLanguage = "ASN.1", 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 Text" -> return () >> pHandleEndLine "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"),("Comment","co"),("Data Type","dt")] parseExpressionInternal = do context <- currentContext parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes)) list_keywords = Set.fromList $ words $ "DEFINITIONS BEGIN END EXPORTS IMPORTS FROM APPLICATION PRIVATE UNIVERSAL DEFAULT OPTIONAL FALSE TRUE" list_types = Set.fromList $ words $ "BOOLEAN INTEGER OCTET STRING NULL REAL ENUMERATED SEQUENCE SET CHOICE OF VisibleString StringStore" defaultAttributes = [("Normal Text","Normal Text"),("Comment","Comment")] parseRules "Normal Text" = do (attr, result) <- (((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute "Keyword")) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_types >>= withAttribute "Data Type")) <|> ((pDetect2Chars False '-' '-' >>= withAttribute "Comment") >>~ pushContext "Comment")) return (attr, result) parseRules "Comment" = pzero parseRules "" = parseRules "Normal Text" parseRules x = fail $ "Unknown context" ++ x