1
1
mirror of https://github.com/github/semantic.git synced 2025-01-06 23:46:21 +03:00
semantic/src/Language.hs

56 lines
1.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
2016-02-11 01:30:14 +03:00
module Language where
import Data.Record
import Info
import Prologue
import Source
import SourceSpan
import qualified Syntax as S
import Term
2016-02-11 01:32:11 +03:00
2016-02-11 01:30:14 +03:00
-- | A programming language.
data Language =
2016-09-08 23:22:05 +03:00
C
| CoffeeScript
| CPlusPlus
| CSharp
| CSS
| Haskell
| HTML
| Java
2016-02-11 01:30:14 +03:00
| JavaScript
2016-08-29 19:47:55 +03:00
| Markdown
| ObjectiveC
| Perl
| PHP
| Python
| R
| Ruby
| Swift
2016-05-26 18:34:02 +03:00
deriving (Show)
2016-02-11 01:32:11 +03:00
-- | Returns a Language based on the file extension (including the ".").
languageForType :: Text -> Maybe Language
languageForType mediaType = case mediaType of
".h" -> Just C
".c" -> Just C
".js" -> Just JavaScript
2016-08-29 19:48:26 +03:00
".md" -> Just Markdown
2016-02-22 06:57:44 +03:00
".rb" -> Just Ruby
2016-02-11 01:32:11 +03:00
_ -> Nothing
termConstructor
:: Source Char -- ^ The source that the term occurs within.
-> IO SourceSpan -- ^ The span that the term occupies. This is passed in 'IO' to guarantee some access constraints & encourage its use only when needed (improving performance).
-> Text -- ^ The name of the production for this node.
-> Range -- ^ The character range that the term occupies.
-> [Term (S.Syntax Text) (Record '[Range, Category])] -- ^ The child nodes of the term.
-> IO (Term (S.Syntax Text) (Record '[Range, Category])) -- ^ The resulting term, in IO.
termConstructor source sourceSpan name range children =
withDefaultInfo <$> case (name, children) of
("ERROR", _) -> S.Error <$> sourceSpan <*> pure children
(_, []) -> S.Leaf <$> pure (toText $ slice range source)
_ -> S.Indexed <$> pure children
where withDefaultInfo syntax = cofree ((range .: Other name .: RNil) :< syntax)