{- This module was generated from data in the Kate syntax highlighting file javascript.xml, version 1.18, by Anders Lund (anders@alweb.dk), Joseph Wenninger (jowenn@kde.org), Whitehawk Stormchaser (zerokode@gmx.net) -} module Text.Highlighting.Kate.Syntax.Javascript (highlight, parseExpression, syntaxName, syntaxExtensions) where import Text.Highlighting.Kate.Types import Text.Highlighting.Kate.Common import qualified Text.Highlighting.Kate.Syntax.Alert import Text.ParserCombinators.Parsec hiding (State) import Control.Monad.State import Data.Char (isSpace) import Data.Maybe (fromMaybe) import qualified Data.Set as Set -- | Full name of language. syntaxName :: String syntaxName = "JavaScript" -- | Filename extensions for this language. syntaxExtensions :: String syntaxExtensions = "*.js" -- | Highlight source code using this syntax definition. highlight :: String -> [SourceLine] highlight input = evalState (mapM parseSourceLine $ lines input) startingState parseSourceLine :: String -> State SyntaxState SourceLine parseSourceLine = mkParseSourceLine parseExpression -- | Parse an expression using appropriate local context. parseExpression :: KateParser Token parseExpression = do (lang,cont) <- currentContext result <- parseRules (lang,cont) optional $ do eof updateState $ \st -> st{ synStPrevChar = '\n' } pEndLine return result startingState = SyntaxState {synStContexts = [("JavaScript","Normal")], synStLineNumber = 0, synStPrevChar = '\n', synStPrevNonspace = False, synStCaseSensitive = True, synStKeywordCaseSensitive = True, synStCaptures = []} pEndLine = do updateState $ \st -> st{ synStPrevNonspace = False } context <- currentContext case context of ("JavaScript","Normal") -> return () ("JavaScript","String") -> (popContext) >> pEndLine ("JavaScript","String 1") -> (popContext) >> pEndLine ("JavaScript","Comment") -> (popContext) >> pEndLine ("JavaScript","Multi/inline Comment") -> return () ("JavaScript","Regular Expression") -> return () ("JavaScript","(Internal regex catch)") -> return () ("JavaScript","Regular Expression Character Class") -> return () ("JavaScript","(regex caret first check)") -> (popContext) >> pEndLine ("JavaScript","(charclass caret first check)") -> (popContext) >> pEndLine ("JavaScript","region_marker") -> (popContext) >> pEndLine ("JavaScript","ObjectMember") -> return () _ -> return () withAttribute attr txt = do when (null txt) $ fail "Parser matched no text" updateState $ \st -> st { synStPrevChar = last txt , synStPrevNonspace = synStPrevNonspace st || not (all isSpace txt) } return (attr, txt) list_keywords = Set.fromList $ words $ "if else for in while do continue break with try catch finally switch case new var function return delete true false void throw typeof const default" list_functions = Set.fromList $ words $ "escape isFinite isNaN Number parseFloat parseInt reload taint unescape untaint write" list_objects = Set.fromList $ words $ "Anchor Applet Area Array Boolean Button Checkbox Date document window Image FileUpload Form Frame Function Hidden Link MimeType Math Max Min Layer navigator Object Password Plugin Radio RegExp Reset Screen Select String Text Textarea this Window" list_math = Set.fromList $ words $ "abs acos asin atan atan2 ceil cos ctg E exp floor LN2 LN10 log LOG2E LOG10E PI pow round sin sqrt SQRT1_2 SQRT2 tan" list_events = Set.fromList $ words $ "onAbort onBlur onChange onClick onError onFocus onLoad onMouseOut onMouseOver onReset onSelect onSubmit onUnload" regex_'5cb'5b'5cw'5c'2e'5d'2b'5cb'5cs'2a'28'3f'3d'3a'29 = compileRegex "\\b[\\w\\.]+\\b\\s*(?=:)" regex_'5cb'5b'5cw'5c'2e'5d'2b'28'3f'3d'5c'2e'29 = compileRegex "\\b[\\w\\.]+(?=\\.)" regex_'5b'3d'3f'3a'5d = compileRegex "[=?:]" regex_'5c'28 = compileRegex "\\(" regex_'2f'5big'5d'7b0'2c2'7d = compileRegex "/[ig]{0,2}" regex_'5c'7b'5b'5cd'2c_'5d'2b'5c'7d = compileRegex "\\{[\\d, ]+\\}" regex_'5c'5c'5bbB'5d = compileRegex "\\\\[bB]" regex_'5c'5c'5bnrtvfDdSsWw'5d = compileRegex "\\\\[nrtvfDdSsWw]" regex_'5c'5c'2e = compileRegex "\\\\." regex_'5c'24'28'3f'3d'2f'29 = compileRegex "\\$(?=/)" regex_'2f'2f'28'3f'3d'3b'29 = compileRegex "//(?=;)" regex_'5c'5c'5b'5c'5b'5c'5d'5d = compileRegex "\\\\[\\[\\]]" defaultAttributes = [(("JavaScript","Normal"),NormalTok),(("JavaScript","String"),StringTok),(("JavaScript","String 1"),CharTok),(("JavaScript","Comment"),CommentTok),(("JavaScript","Multi/inline Comment"),CommentTok),(("JavaScript","Regular Expression"),OtherTok),(("JavaScript","(Internal regex catch)"),NormalTok),(("JavaScript","Regular Expression Character Class"),BaseNTok),(("JavaScript","(regex caret first check)"),FloatTok),(("JavaScript","(charclass caret first check)"),FloatTok),(("JavaScript","region_marker"),RegionMarkerTok),(("JavaScript","ObjectMember"),NormalTok)] parseRules ("JavaScript","Normal") = (((pDetectSpaces >>= withAttribute NormalTok)) <|> ((pString False "//BEGIN" >>= withAttribute RegionMarkerTok) >>~ pushContext ("JavaScript","region_marker")) <|> ((pString False "//END" >>= withAttribute RegionMarkerTok) >>~ pushContext ("JavaScript","region_marker")) <|> ((pFloat >>= withAttribute FloatTok)) <|> ((pInt >>= withAttribute DecValTok)) <|> ((pRegExpr regex_'5cb'5b'5cw'5c'2e'5d'2b'5cb'5cs'2a'28'3f'3d'3a'29 >>= withAttribute DataTypeTok)) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_keywords >>= withAttribute KeywordTok)) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_functions >>= withAttribute FunctionTok)) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_objects >>= withAttribute KeywordTok)) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_math >>= withAttribute KeywordTok)) <|> ((pKeyword " \n\t.():!+,-<=>%&*/;?[]^{|}~\\" list_events >>= withAttribute KeywordTok)) <|> ((pDetectChar False '.' >>= withAttribute NormalTok) >>~ pushContext ("JavaScript","ObjectMember")) <|> ((pRegExpr regex_'5cb'5b'5cw'5c'2e'5d'2b'28'3f'3d'5c'2e'29 >>= withAttribute KeywordTok) >>~ pushContext ("JavaScript","ObjectMember")) <|> ((pDetectIdentifier >>= withAttribute NormalTok)) <|> ((pDetectChar False '"' >>= withAttribute StringTok) >>~ pushContext ("JavaScript","String")) <|> ((pDetectChar False '\'' >>= withAttribute CharTok) >>~ pushContext ("JavaScript","String 1")) <|> ((pDetect2Chars False '/' '/' >>= withAttribute CommentTok) >>~ pushContext ("JavaScript","Comment")) <|> ((pDetect2Chars False '/' '*' >>= withAttribute CommentTok) >>~ pushContext ("JavaScript","Multi/inline Comment")) <|> ((pRegExpr regex_'5b'3d'3f'3a'5d >>= withAttribute NormalTok) >>~ pushContext ("JavaScript","(Internal regex catch)")) <|> ((pRegExpr regex_'5c'28 >>= withAttribute NormalTok) >>~ pushContext ("JavaScript","(Internal regex catch)")) <|> ((pDetectChar False '{' >>= withAttribute NormalTok)) <|> ((pDetectChar False '}' >>= withAttribute NormalTok)) <|> ((pAnyChar ":!%&+,-/.*<=>?[]|~^;" >>= withAttribute NormalTok)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","Normal")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","Normal") defaultAttributes))) parseRules ("JavaScript","String") = (((pDetectIdentifier >>= withAttribute StringTok)) <|> ((pHlCStringChar >>= withAttribute CharTok)) <|> ((pLineContinue >>= withAttribute StringTok)) <|> ((pDetectChar False '"' >>= withAttribute StringTok) >>~ (popContext)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","String")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","String") defaultAttributes))) parseRules ("JavaScript","String 1") = (((pDetectIdentifier >>= withAttribute CharTok)) <|> ((pHlCStringChar >>= withAttribute CharTok)) <|> ((pLineContinue >>= withAttribute StringTok)) <|> ((pDetectChar False '\'' >>= withAttribute CharTok) >>~ (popContext)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","String 1")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","String 1") defaultAttributes))) parseRules ("JavaScript","Comment") = (((pDetectSpaces >>= withAttribute CommentTok)) <|> ((Text.Highlighting.Kate.Syntax.Alert.parseExpression >>= ((withAttribute CommentTok) . snd))) <|> ((pDetectIdentifier >>= withAttribute CommentTok)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","Comment")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","Comment") defaultAttributes))) parseRules ("JavaScript","Multi/inline Comment") = (((Text.Highlighting.Kate.Syntax.Alert.parseExpression >>= ((withAttribute CommentTok) . snd))) <|> ((pDetect2Chars False '*' '/' >>= withAttribute CommentTok) >>~ (popContext)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","Multi/inline Comment")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","Multi/inline Comment") defaultAttributes))) parseRules ("JavaScript","Regular Expression") = (((pRegExpr regex_'2f'5big'5d'7b0'2c2'7d >>= withAttribute OtherTok) >>~ (popContext >> popContext >> popContext)) <|> ((pRegExpr regex_'5c'7b'5b'5cd'2c_'5d'2b'5c'7d >>= withAttribute FloatTok)) <|> ((pRegExpr regex_'5c'5c'5bbB'5d >>= withAttribute FloatTok)) <|> ((pRegExpr regex_'5c'5c'5bnrtvfDdSsWw'5d >>= withAttribute BaseNTok)) <|> ((pDetectChar False '[' >>= withAttribute BaseNTok) >>~ pushContext ("JavaScript","(charclass caret first check)")) <|> ((pRegExpr regex_'5c'5c'2e >>= withAttribute FloatTok)) <|> ((pRegExpr regex_'5c'24'28'3f'3d'2f'29 >>= withAttribute FloatTok)) <|> ((pAnyChar "?+*()|" >>= withAttribute FloatTok)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","Regular Expression")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","Regular Expression") defaultAttributes))) parseRules ("JavaScript","(Internal regex catch)") = (((pDetectSpaces >>= withAttribute NormalTok)) <|> ((pRegExpr regex_'2f'2f'28'3f'3d'3b'29 >>= withAttribute OtherTok) >>~ (popContext)) <|> ((pDetect2Chars False '/' '/' >>= withAttribute CommentTok) >>~ pushContext ("JavaScript","Comment")) <|> ((pDetect2Chars False '/' '*' >>= withAttribute CommentTok) >>~ pushContext ("JavaScript","Multi/inline Comment")) <|> ((pDetectChar False '/' >>= withAttribute OtherTok) >>~ pushContext ("JavaScript","(regex caret first check)")) <|> ((popContext) >> currentContext >>= parseRules)) parseRules ("JavaScript","Regular Expression Character Class") = (((pRegExpr regex_'5c'5c'5b'5c'5b'5c'5d'5d >>= withAttribute BaseNTok)) <|> ((pRegExpr regex_'5c'5c'2e >>= withAttribute FloatTok)) <|> ((pDetectChar False ']' >>= withAttribute BaseNTok) >>~ (popContext >> popContext)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","Regular Expression Character Class")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","Regular Expression Character Class") defaultAttributes))) parseRules ("JavaScript","(regex caret first check)") = (((pDetectChar False '^' >>= withAttribute FloatTok) >>~ pushContext ("JavaScript","Regular Expression")) <|> (pushContext ("JavaScript","Regular Expression") >> currentContext >>= parseRules)) parseRules ("JavaScript","(charclass caret first check)") = (((pDetectChar False '^' >>= withAttribute FloatTok) >>~ pushContext ("JavaScript","Regular Expression Character Class")) <|> (pushContext ("JavaScript","Regular Expression Character Class") >> currentContext >>= parseRules)) parseRules ("JavaScript","region_marker") = (((pDetectIdentifier >>= withAttribute RegionMarkerTok)) <|> ((pDetectSpaces >>= withAttribute RegionMarkerTok)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","region_marker")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","region_marker") defaultAttributes))) parseRules ("JavaScript","ObjectMember") = (((pDetectChar False '.' >>= withAttribute NormalTok)) <|> ((pDetectIdentifier >>= withAttribute FunctionTok)) <|> ((pDetectSpaces >>= withAttribute NormalTok) >>~ (popContext)) <|> ((lookAhead (pAnyChar "(){}:!%&+,-/.*<=>?[]|~^;") >> (popContext) >> currentContext >>= parseRules)) <|> (currentContext >>= \x -> guard (x == ("JavaScript","ObjectMember")) >> pDefault >>= withAttribute (fromMaybe NormalTok $ lookup ("JavaScript","ObjectMember") defaultAttributes))) parseRules ("Alerts", _) = Text.Highlighting.Kate.Syntax.Alert.parseExpression parseRules x = parseRules ("JavaScript","Normal") <|> fail ("Unknown context" ++ show x)