language-typescript (empty) → 0.0.1
raw patch · 7 files changed
+454/−0 lines, 7 filesdep +basedep +containersdep +parsecsetup-changed
Dependencies added: base, containers, parsec
Files
- LICENSE +20/−0
- Setup.lhs +6/−0
- language-typescript.cabal +25/−0
- src/Language/TypeScript.hs +23/−0
- src/Language/TypeScript/Lexer.hs +92/−0
- src/Language/TypeScript/Parser.hs +174/−0
- src/Language/TypeScript/Types.hs +114/−0
+ LICENSE view
@@ -0,0 +1,20 @@+The MIT License (MIT)++Copyright (c) 2013 Phil Freeman++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+the Software, and to permit persons to whom the Software is furnished to do so,+subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.lhs view
@@ -0,0 +1,6 @@+#!/usr/bin/runhaskell +> module Main where+> import Distribution.Simple+> main :: IO ()+> main = defaultMain+
+ language-typescript.cabal view
@@ -0,0 +1,25 @@+name: language-typescript+version: 0.0.1+cabal-version: >=1.4+build-type: Simple+license: MIT+license-file: LICENSE+copyright: (c) DICOM Grid Inc. 2013+maintainer: Phillip Freeman <paf31@cantab.net>+stability: experimental+homepage: http://github.com/paf31/language-typescript+synopsis: A library for working with TypeScript Definition files+description: A library for working with TypeScript Definition files+category: Language+author: Phillip Freeman <paf31@cantab.net>+data-dir: ""++library+ build-depends: base >= 3 && <= 5, containers -any, parsec -any+ exposed-modules: Language.TypeScript Language.TypeScript.Parser+ Language.TypeScript.Lexer Language.TypeScript.Types+ exposed: True+ buildable: True+ hs-source-dirs: src+ other-modules:+
+ src/Language/TypeScript.hs view
@@ -0,0 +1,23 @@+-----------------------------------------------------------------------------+--+-- Module : Language.TypeScript+-- Copyright : (c) DICOM Grid Inc. 2013+-- License : MIT+--+-- Maintainer : Phillip Freeman <paf31@cantab.net>+-- Stability : experimental+-- Portability :+--+-- |+--+-----------------------------------------------------------------------------++module Language.TypeScript (+ module Language.TypeScript.Types,+ module Language.TypeScript.Lexer,+ module Language.TypeScript.Parser+) where++import Language.TypeScript.Types+import Language.TypeScript.Lexer+import Language.TypeScript.Parser
+ src/Language/TypeScript/Lexer.hs view
@@ -0,0 +1,92 @@+-----------------------------------------------------------------------------+--+-- Module : Language.TypeScript.Lexer+-- Copyright : (c) DICOM Grid Inc. 2013+-- License : MIT+--+-- Maintainer : Phillip Freeman <paf31@cantab.net>+-- Stability : experimental+-- Portability :+--+-- |+--+-----------------------------------------------------------------------------++module Language.TypeScript.Lexer (+ identifier+ , reserved+ , operator+ , reservedOp+ , charLiteral+ , stringLiteral+ , natural+ , integer+ , float+ , naturalOrFloat+ , decimal+ , hexadecimal+ , octal+ , symbol+ , lexeme+ , whiteSpace+ , parens+ , braces+ , angles+ , brackets+ , squares+ , semi+ , comma+ , colon+ , dot+ , semiSep+ , semiSep1+ , commaSep+ , commaSep1+) where++import Text.Parsec+import qualified Text.Parsec.Token as T+import Text.Parsec.Language++typeScriptDef = javaStyle+ { T.identStart = oneOf "_$" <|> letter+ , T.reservedNames = [+ "break", "do", "instanceof", "typeof", "case", "else", "new", "var", "catch", "finally", "return", "void", "continue", "for",+ "switch", "while", "debugger", "function", "this", "with", "default", "if", "throw", "delete", "in", "try", "class", "enum",+ "extends", "super", "const", "export", "import", "implements", "let", "private", "public", "yield", "interface", "package",+ "protected", "static"+ ]+ , T.caseSensitive = True+ }++parser = T.makeTokenParser typeScriptDef++identifier = T.identifier parser+reserved = T.reserved parser+operator = T.operator parser+reservedOp = T.reservedOp parser+charLiteral = T.charLiteral parser+stringLiteral = T.stringLiteral parser+natural = T.natural parser+integer = T.integer parser+float = T.float parser+naturalOrFloat = T.naturalOrFloat parser+decimal = T.decimal parser+hexadecimal = T.hexadecimal parser+octal = T.octal parser+symbol = T.symbol parser+lexeme = T.lexeme parser+whiteSpace = T.whiteSpace parser+parens = T.parens parser+braces = T.braces parser+angles = T.angles parser+brackets = T.brackets parser+squares = T.brackets parser+semi = T.semi parser+comma = T.comma parser+colon = T.colon parser+dot = T.dot parser+semiSep = T.semiSep parser+semiSep1 = T.semiSep1 parser+commaSep = T.commaSep parser+commaSep1 = T.commaSep1 parser
+ src/Language/TypeScript/Parser.hs view
@@ -0,0 +1,174 @@+-----------------------------------------------------------------------------+--+-- Module : Language.TypeScript.Parser+-- Copyright : (c) DICOM Grid Inc. 2013+-- License : MIT+--+-- Maintainer : Phillip Freeman <paf31@cantab.net>+-- Stability : experimental+-- Portability :+--+-- |+--+-----------------------------------------------------------------------------++module Language.TypeScript.Parser (+ declarationSourceFile,+ nextIdentifier+) where++import Language.TypeScript.Types+import Language.TypeScript.Lexer++import Text.Parsec+import Text.Parsec.Expr+import Text.Parsec.String (parseFromFile)+import Control.Applicative+ (Applicative(..), (<$>), (<*>), (<*), (*>))++commentPlaceholder = fmap toOffset getPosition where+ toOffset pos = Left $ (sourceLine pos, sourceColumn pos)++nextIdentifier =+ skipMany (choice (map (try . reserved) [ "export", "declare", "public", "private", "static" ]))+ >> choice (map (try . reserved) [ "var", "function", "class", "interface", "enum", "module" ])+ >> identifier++declarationSourceFile = whiteSpace >> many declarationElement <* eof++declarationElement = choice $ map try+ [ InterfaceDeclaration <$> commentPlaceholder <*> optionMaybe (reserved "export" >> return Exported) <*> interface+ , ImportDeclaration <$> optionMaybe (reserved "export" >> return Exported) <*> (reserved "import" *> identifier) <*> (lexeme (char '=') *> entityName)+ , ExportDeclaration <$> (reserved "export" >> lexeme (char '=') *> identifier)+ , ExternalImportDeclaration <$> optionMaybe (reserved "export" >> return Exported) <*> (reserved "import" *> identifier) <*> (lexeme (char '=') *> reserved "require" *> parens stringLiteral)+ , AmbientDeclaration <$> commentPlaceholder <*> optionMaybe (reserved "export" >> return Exported) <*> (reserved "declare" *> ambientDeclaration)+ ]++ambientDeclaration = choice (map try+ [ ambientVariableDeclaration+ , ambientFunctionDeclaration+ , ambientClassDeclaration+ , ambientInterfaceDeclaration+ , ambientEnumDeclaration+ , ambientModuleDeclaration+ , ambientExternalModuleDeclaration+ ])++ambientVariableDeclaration = AmbientVariableDeclaration <$> commentPlaceholder <*> (reserved "var" *> identifier) <*> (optionMaybe typeAnnotation <* semi)++ambientFunctionDeclaration = AmbientFunctionDeclaration <$> commentPlaceholder <*> (reserved "function" *> identifier) <*> (parameterListAndReturnType <* semi)++ambientClassDeclaration = AmbientClassDeclaration <$> commentPlaceholder <*> (reserved "class" *> identifier) <*> optionMaybe typeParameters <*> optionMaybe extendsClause <*> optionMaybe implementsClause <*> braces (sepEndBy ambientClassBodyElement semi)++ambientInterfaceDeclaration = AmbientInterfaceDeclaration <$> interface++ambientEnumDeclaration = AmbientEnumDeclaration <$> commentPlaceholder <*> (reserved "enum" *> identifier) <*> braces (sepEndBy enumMember comma)+ where+ enumMember = (,) <$> propertyName <*> optionMaybe (lexeme (char '=') >> integer)++ambientModuleDeclaration = AmbientModuleDeclaration <$> commentPlaceholder <*> (reserved "module" *> sepBy identifier dot) <*> braces (many ambientDeclaration)++ambientExternalModuleDeclaration = AmbientExternalModuleDeclaration <$> commentPlaceholder <*> (reserved "module" *> stringLiteral) <*> braces (many ambientDeclaration)++ambientClassBodyElement = (,) <$> commentPlaceholder <*> (choice $ map try+ [ ambientConstructorDeclaration+ , ambientMemberDeclaration+ , ambientIndexSignature ])++ambientConstructorDeclaration = AmbientConstructorDeclaration <$> (reserved "constructor" *> parameterList <* semi)++ambientMemberDeclaration = AmbientMemberDeclaration <$> optionMaybe publicOrPrivate <*> optionMaybe static <*> propertyName <*> choice [fmap Right parameterListAndReturnType, fmap Left (optionMaybe typeAnnotation)]++ambientIndexSignature = AmbientIndexSignature <$> indexSignature++interface = Interface <$> commentPlaceholder <*> (reserved "interface" *> identifier) <*> optionMaybe typeParameters <*> optionMaybe extendsClause <*> objectType++extendsClause = reserved "extends" >> classOrInterfaceTypeList++implementsClause = reserved "implements" >> classOrInterfaceTypeList++classOrInterfaceTypeList = commaSep typeRef++objectType = braces typeBody++typeBody = TypeBody <$> sepEndBy typeMember semi+ where+ typeMember = (,) <$> commentPlaceholder <*> (choice $ map try [ methodSignature, propertySignature, callSignature, constructSignature, typeIndexSignature ])++propertySignature = PropertySignature <$> propertyName <*> optionMaybe (lexeme (char '?' >> return Optional)) <*> optionMaybe typeAnnotation++propertyName = identifier <|> stringLiteral++typeAnnotation = colon >> _type++callSignature = CallSignature <$> parameterListAndReturnType++parameterListAndReturnType = ParameterListAndReturnType <$> optionMaybe typeParameters <*> parens parameterList <*> optionMaybe typeAnnotation++parameterList = commaSep parameter++parameter = choice+ [ try $ RequiredOrOptionalParameter <$> optionMaybe publicOrPrivate <*> identifier <*> optionMaybe (lexeme (char '?' >> return Optional)) <*> optionMaybe typeAnnotation+ , RestParameter <$> (lexeme (string "...") *> identifier) <*> optionMaybe typeAnnotation+ ]++static = reserved "static" >> return Static++publicOrPrivate = choice+ [ reserved "public" >> return Public+ , reserved "private" >> return Private ]++stringOrNumber = choice+ [ reserved "string" >> return String+ , reserved "number" >> return Number ]++constructSignature = ConstructSignature <$> (reserved "new" *> optionMaybe typeParameters) <*> parens parameterList <*> optionMaybe typeAnnotation++typeIndexSignature = TypeIndexSignature <$> indexSignature++indexSignature = squares (IndexSignature <$> identifier <*> (colon *> stringOrNumber)) <*> typeAnnotation++methodSignature = MethodSignature <$> propertyName <*> optionMaybe (lexeme (char '?' >> return Optional)) <*> parameterListAndReturnType++typeParameters = angles $ commaSep1 typeParameter++typeParameter = TypeParameter <$> identifier <*> optionMaybe (reserved "extends" >> _type)+fold :: Stream s m t => ParsecT s u m a -> ParsecT s u m b -> (a -> b -> a) -> ParsecT s u m a+fold first more combine = do+ a <- first+ bs <- many more+ return $ foldl combine a bs++_type = lexeme $ choice [ arrayType, functionType, constructorType ]+ where+ arrayType = fold atomicType (squares whiteSpace) (flip $ const ArrayType)+ atomicType = choice $ map try+ [ Predefined <$> predefinedType+ , TypeReference <$> typeRef+ , ObjectType <$> objectType+ ]+ functionType = FunctionType <$> optionMaybe typeParameters <*> parens parameterList <*> returnType+ constructorType = ConstructorType <$> (reserved "new" *> optionMaybe typeParameters) <*> parens parameterList <*> returnType+ returnType = lexeme (string "=>") *> _type++typeRef = TypeRef <$> typeName <*> optionMaybe typeArguments++predefinedType = choice+ [ reserved "any" >> return AnyType+ , reserved "number" >> return NumberType+ , (reserved "boolean" <|> reserved "bool") >> return BooleanType+ , reserved "string" >> return StringType+ , reserved "void" >> return VoidType+ ]++entityName = fmap toEntityName (sepBy1 identifier dot)+ where+ toEntityName [t] = EntityName Nothing t+ toEntityName ts = EntityName (Just $ ModuleName $ init ts) (last ts)++typeName = fmap toTypeName (sepBy1 identifier dot)+ where+ toTypeName [t] = TypeName Nothing t+ toTypeName ts = TypeName (Just $ ModuleName $ init ts) (last ts)++typeArguments = angles $ commaSep1 _type
+ src/Language/TypeScript/Types.hs view
@@ -0,0 +1,114 @@+-----------------------------------------------------------------------------+--+-- Module : Language.TypeScript.Types+-- Copyright : (c) DICOM Grid Inc. 2013+-- License : MIT+--+-- Maintainer : Phillip Freeman <paf31@cantab.net>+-- Stability : experimental+-- Portability :+--+-- |+--+-----------------------------------------------------------------------------++{-# LANGUAGE DeriveDataTypeable #-}++module Language.TypeScript.Types where++import qualified Data.Map as M+import Data.Monoid+import Data.Data (Typeable, Data)++data Comment = Comment+ { text :: [String]+ , other :: [(String, String)]+ } deriving (Show, Data, Typeable)++instance Monoid Comment where+ mempty = Comment [] []+ mappend (Comment ts os) (Comment ts' os') = Comment (ts ++ ts') (os ++ os')++type CommentPlaceholder = Either (Int, Int) Comment++data DeclarationElement+ = InterfaceDeclaration CommentPlaceholder (Maybe Exported) Interface+ | ImportDeclaration (Maybe Exported) String EntityName+ | ExportDeclaration String+ | ExternalImportDeclaration (Maybe Exported) String String+ | AmbientDeclaration CommentPlaceholder (Maybe Exported) Ambient+ deriving (Show, Data, Typeable)++data Exported = Exported deriving (Show, Data, Typeable)++data EntityName = EntityName (Maybe ModuleName) String deriving (Show, Data, Typeable)++data Interface = Interface CommentPlaceholder String (Maybe [TypeParameter]) (Maybe [TypeRef]) TypeBody deriving (Show, Data, Typeable)++data Ambient+ = AmbientVariableDeclaration CommentPlaceholder String (Maybe Type)+ | AmbientFunctionDeclaration CommentPlaceholder String ParameterListAndReturnType+ | AmbientClassDeclaration CommentPlaceholder String (Maybe [TypeParameter]) (Maybe [TypeRef]) (Maybe [TypeRef]) [(CommentPlaceholder, AmbientClassBodyElement)]+ | AmbientInterfaceDeclaration Interface+ | AmbientEnumDeclaration CommentPlaceholder String [(String, Maybe Integer)]+ | AmbientModuleDeclaration CommentPlaceholder [String] [Ambient]+ | AmbientExternalModuleDeclaration CommentPlaceholder String [Ambient]+ deriving (Show, Data, Typeable)++data TypeRef = TypeRef TypeName (Maybe [Type]) deriving (Show, Data, Typeable)++data AmbientClassBodyElement+ = AmbientConstructorDeclaration [Parameter]+ | AmbientMemberDeclaration (Maybe PublicOrPrivate) (Maybe Static) String (Either (Maybe Type) ParameterListAndReturnType)+ | AmbientIndexSignature IndexSignature+ deriving (Show, Data, Typeable)++data Static = Static deriving (Show, Data, Typeable)++data Optional = Optional deriving (Show, Data, Typeable)++data TypeBody = TypeBody [(CommentPlaceholder, TypeMember)] deriving (Show, Data, Typeable)++data TypeMember+ = PropertySignature String (Maybe Optional) (Maybe Type)+ | CallSignature ParameterListAndReturnType+ | ConstructSignature (Maybe [TypeParameter]) [Parameter] (Maybe Type)+ | TypeIndexSignature IndexSignature+ | MethodSignature String (Maybe Optional) ParameterListAndReturnType+ deriving (Show, Data, Typeable)++data IndexSignature = IndexSignature String StringOrNumber Type deriving (Show, Data, Typeable)++data ParameterListAndReturnType = ParameterListAndReturnType (Maybe [TypeParameter]) [Parameter] (Maybe Type) deriving (Show, Data, Typeable)++data Parameter+ = RequiredOrOptionalParameter (Maybe PublicOrPrivate) String (Maybe Optional) (Maybe Type)+ | RestParameter String (Maybe Type)+ deriving (Show, Data, Typeable)++data StringOrNumber = String | Number deriving (Show, Data, Typeable)++data PublicOrPrivate = Public | Private deriving (Show, Data, Typeable)++data TypeParameter = TypeParameter String (Maybe Type) deriving (Show, Data, Typeable)++data Type+ = Predefined PredefinedType+ | TypeReference TypeRef+ | ObjectType TypeBody+ | ArrayType Type+ | FunctionType (Maybe [TypeParameter]) [Parameter] Type+ | ConstructorType (Maybe [TypeParameter]) [Parameter] Type+ deriving (Show, Data, Typeable)++data TypeName = TypeName (Maybe ModuleName) String deriving (Show, Data, Typeable)++data ModuleName = ModuleName [String] deriving (Show, Data, Typeable)++data PredefinedType+ = AnyType+ | NumberType+ | BooleanType+ | StringType+ | VoidType+ deriving (Show, Data, Typeable)