{- |
   Module      : Text.Highlighting.Kate.Syntax
   Copyright   : Copyright (C) 2008-2011 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Token lexer for various languages.
-}

module Text.Highlighting.Kate.Syntax ( highlightAs, languages, languagesByExtension,
                                       languagesByFilename ) where
import Data.Char (toLower)
import Text.Highlighting.Kate.Types
import Text.Highlighting.Kate.Common (matchGlobs)
@imports@

-- | List of supported languages.
languages :: [String]
languages = @languages@

-- | List of language extensions.
languageExtensions :: [(String, String)]
languageExtensions = @languageExtensions@

-- | Returns a list of languages appropriate for the given file extension.
languagesByExtension :: String -> [String]
languagesByExtension ('.':ext) = languagesByFilename ("*." ++ ext)
languagesByExtension ext       = languagesByFilename ("*." ++ ext)

-- | Returns a list of languages appropriate for the given filename.
languagesByFilename :: FilePath -> [String]
languagesByFilename fn = [lang | (lang, globs) <- languageExtensions, matchGlobs fn globs]

-- | Highlight source code. The source language may be specified
-- by its canonical name (case-insensitive) or by a canonical
-- extension (if unique).
-- The parsers read the input lazily and parse line by line;
-- results are returned immediately.
-- Supported languages: @supportedlanguages@.
highlightAs :: String         -- ^ Language syntax (e.g. "haskell") or extension (e.g. "hs").
            -> String         -- ^ Source code to highlight
            -> [SourceLine]   -- ^ List of highlighted source lines
highlightAs lang =
  let lang'  = map toLower lang
      lang'' = if lang' `elem` map (map toLower) languages
                  then lang'
                  else case languagesByExtension lang' of
                            [l]  -> map toLower l  -- go by extension if unambiguous
                            _    -> lang'
  in  case lang'' of
        @cases@
        _ -> map (\l -> [(NormalTok, l)]) . lines
