packages feed

language-elm 0.0.1.0 → 0.0.2.0

raw patch · 13 files changed

+322/−340 lines, 13 filesdep −regex-compatdep ~HUnitdep ~basePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies removed: regex-compat

Dependency ranges changed: HUnit, base

API changes (from Hackage documentation)

- Decleration: Dec :: String -> TypeDec -> [Expr] -> Expr -> Dec
- Decleration: DecType :: String -> [String] -> [(String, [TypeDec])] -> Dec
- Decleration: DecTypeAlias :: String -> [String] -> TypeDec -> Dec
- Decleration: data Dec
- Decleration: toDocD :: Dec -> Doc
- Expression: App :: String -> [Expr] -> Expr
- Expression: BoolFalse :: Expr
- Expression: BoolTrue :: Expr
- Expression: Case :: Expr -> [(Expr, Expr)] -> Expr
- Expression: Int :: Int -> Expr
- Expression: Let :: Expr -> [(Expr, Expr)] -> Expr
- Expression: List :: [Expr] -> Expr
- Expression: Op :: String -> Expr -> Expr -> Expr
- Expression: Parens :: Expr -> Expr
- Expression: Record :: (Maybe Expr) -> [(String, Expr)] -> Expr
- Expression: Str :: String -> Expr
- Expression: Tuple2 :: Expr -> Expr -> Expr
- Expression: Tuple3 :: Expr -> Expr -> Expr -> Expr
- Expression: Under :: Expr
- Expression: data Expr
- Expression: toDoc :: Expr -> Doc
- Expression: var :: String -> Expr
- Expression: vop :: Expr -> Doc
- Import: Everything :: ImportType
- Import: ExposeNothing :: ImportType
- Import: Import :: String -> (Maybe String) -> ImportType -> Import
- Import: Item :: String -> ImportItem
- Import: ItemEvery :: String -> ImportItem
- Import: ItemExposing :: String -> [String] -> ImportItem
- Import: Select :: [ImportItem] -> ImportType
- Import: data Import
- Import: data ImportItem
- Import: data ImportType
- Import: docItem :: ImportItem -> Doc
- Import: exposingDoc :: ImportType -> Doc
- Import: toDocI :: Import -> Doc
- Program: Program :: String -> ImportType -> [Import] -> [Dec] -> Program
- Program: data Program
- Program: genProgram :: Program -> Doc
- Program: renderProgram :: Program -> String
- Type: Params :: String -> [TypeDec] -> TypeDec
- Type: TApp :: [TypeDec] -> TypeDec
- Type: TRecord :: (Maybe String) -> [(String, TypeDec)] -> TypeDec
- Type: TTuple2 :: TypeDec -> TypeDec -> TypeDec
- Type: data TypeDec
- Type: toDocT :: TypeDec -> Doc
- Type: tvar :: String -> TypeDec
- Type: vopParam :: TypeDec -> Doc
- Type: vopTApp :: TypeDec -> Doc
+ Elm.Decleration: Dec :: String -> TypeDec -> [Expr] -> Expr -> Dec
+ Elm.Decleration: DecType :: String -> [String] -> [(String, [TypeDec])] -> Dec
+ Elm.Decleration: DecTypeAlias :: String -> [String] -> TypeDec -> Dec
+ Elm.Decleration: data Dec
+ Elm.Decleration: toDocD :: Dec -> Doc
+ Elm.Expression: App :: String -> [Expr] -> Expr
+ Elm.Expression: BoolFalse :: Expr
+ Elm.Expression: BoolTrue :: Expr
+ Elm.Expression: Case :: Expr -> [(Expr, Expr)] -> Expr
+ Elm.Expression: Int :: Int -> Expr
+ Elm.Expression: Let :: Expr -> [(Expr, Expr)] -> Expr
+ Elm.Expression: List :: [Expr] -> Expr
+ Elm.Expression: Op :: String -> Expr -> Expr -> Expr
+ Elm.Expression: Parens :: Expr -> Expr
+ Elm.Expression: Record :: (Maybe Expr) -> [(String, Expr)] -> Expr
+ Elm.Expression: Str :: String -> Expr
+ Elm.Expression: Tuple2 :: Expr -> Expr -> Expr
+ Elm.Expression: Tuple3 :: Expr -> Expr -> Expr -> Expr
+ Elm.Expression: Under :: Expr
+ Elm.Expression: data Expr
+ Elm.Expression: toDoc :: Expr -> Doc
+ Elm.Expression: var :: String -> Expr
+ Elm.Expression: vop :: Expr -> Doc
+ Elm.Import: Everything :: ImportType
+ Elm.Import: ExposeNothing :: ImportType
+ Elm.Import: Import :: String -> (Maybe String) -> ImportType -> Import
+ Elm.Import: Item :: String -> ImportItem
+ Elm.Import: ItemEvery :: String -> ImportItem
+ Elm.Import: ItemExposing :: String -> [String] -> ImportItem
+ Elm.Import: Select :: [ImportItem] -> ImportType
+ Elm.Import: data Import
+ Elm.Import: data ImportItem
+ Elm.Import: data ImportType
+ Elm.Import: docItem :: ImportItem -> Doc
+ Elm.Import: exposingDoc :: ImportType -> Doc
+ Elm.Import: toDocI :: Import -> Doc
+ Elm.Program: Program :: String -> ImportType -> [Import] -> [Dec] -> Program
+ Elm.Program: data Program
+ Elm.Program: genProgram :: Program -> Doc
+ Elm.Program: renderProgram :: Program -> String
+ Elm.Type: Params :: String -> [TypeDec] -> TypeDec
+ Elm.Type: TApp :: [TypeDec] -> TypeDec
+ Elm.Type: TRecord :: (Maybe String) -> [(String, TypeDec)] -> TypeDec
+ Elm.Type: TTuple2 :: TypeDec -> TypeDec -> TypeDec
+ Elm.Type: data TypeDec
+ Elm.Type: toDocT :: TypeDec -> Doc
+ Elm.Type: tvar :: String -> TypeDec
+ Elm.Type: vopParam :: TypeDec -> Doc
+ Elm.Type: vopTApp :: TypeDec -> Doc

Files

− app/Main.hs
@@ -1,4 +0,0 @@-module Main where--main :: IO ()-main = putStrLn "Fuck"
language-elm.cabal view
@@ -1,48 +1,37 @@-name:                language-elm-version:             0.0.1.0-synopsis:            Generate elm code-description:         Generate elm code from an ast-homepage:            https://github.com/eliaslfox/language-elm#readme-license:             BSD3-license-file:        LICENSE-author:              Elias Lawson-Fox-maintainer:          eliaslfox@gmail.com-copyright:           2017 Elias Lawson-Fox-category:            Web-build-type:          Simple-extra-source-files:  README.md-cabal-version:       >=1.10+name:               language-elm+version:            0.0.2.0+synopsis:           Generate elm code+description:        Generate elm code from an ast+homepage:           https://github.com/eliaslfox/language-elm#readme+license:            BSD3+license-file:       LICENSE+author:             Elias Lawson-Fox+maintainer:         eliaslfox@gmail.com+copyright:          2017 Elias Lawson-Fox+category:           Web+build-type:         Simple+extra-source-files: README.md+cabal-version:      >=1.10  library-  hs-source-dirs:      src-  exposed-modules:     Expression, Decleration, Type, Import, Program-  build-depends:       base >= 4.7 && < 5, -                       HUnit >= 1 && < 2, +  hs-source-dirs:   src+  exposed-modules:  Elm.Expression, Elm.Decleration, Elm.Type, Elm.Import, Elm.Program+  build-depends:    base >= 4.7 && < 5,                         pretty,-                       MissingH,-                       regex-compat-  default-language:    Haskell2010--executable language-elm-exe-  hs-source-dirs:      app-  main-is:             Main.hs-  ghc-options:         -threaded -rtsopts -with-rtsopts=-N-  build-depends:       base-                     , language-elm-  default-language:    Haskell2010+                       MissingH+  default-language: Haskell2010  test-suite language-elm-test-  type:                exitcode-stdio-1.0-  hs-source-dirs:      test-  main-is:             Spec.hs-  build-depends:       base+  type:             exitcode-stdio-1.0+  hs-source-dirs:   test+  main-is:          Spec.hs+  build-depends:    base                      , language-elm                      , HUnit                      , pretty-                     , MissingH-  ghc-options:         -threaded -rtsopts -with-rtsopts=-N-  default-language:    Haskell2010+  ghc-options:      -threaded -rtsopts -with-rtsopts=-N+  default-language: Haskell2010  source-repository head-  type:     git-  location: https://github.com/eliaslfox/language-elm+  type:             git+  location:         https://github.com/eliaslfox/language-elm
− src/Decleration.hs
@@ -1,28 +0,0 @@-module Decleration where--import Type-import Expression-import Text.PrettyPrint--data Dec-    = Dec String TypeDec [Expr] Expr-    | DecType String [String] [(String, [TypeDec])]-    | DecTypeAlias String [String] TypeDec--toDocD :: Dec -> Doc-toDocD dec =-    case dec of-        Dec str typeDec args body ->-               text str <+> text "::" <+> toDocT typeDec $+$-               hang (text str <+> (hsep . map toDoc $ args) <+> text "=") 4 (toDoc body)--        DecTypeAlias str typeParams t->-            text "type alias" <+> text str <+> (hsep . map text $ typeParams) <+> text "=" <+> toDocT t--        DecType str typeParams types ->-            text "type" <+> text str <+> (hsep . map text $ typeParams) <+> text "=" <+>-                (hsep . punctuate (text " |") . map toDec $ types)--            where-                toDec (str, t) =-                    text str <+> (hsep . map vopParam $ t)
+ src/Elm/Decleration.hs view
@@ -0,0 +1,28 @@+module Elm.Decleration where++import Elm.Type+import Elm.Expression+import Text.PrettyPrint++data Dec+    = Dec String TypeDec [Expr] Expr+    | DecType String [String] [(String, [TypeDec])]+    | DecTypeAlias String [String] TypeDec++toDocD :: Dec -> Doc+toDocD dec =+    case dec of+        Dec str typeDec args body ->+               text str <+> text "::" <+> toDocT typeDec $+$+               hang (text str <+> (hsep . map toDoc $ args) <+> text "=") 4 (toDoc body)++        DecTypeAlias str typeParams t->+            text "type alias" <+> text str <+> (hsep . map text $ typeParams) <+> text "=" <+> toDocT t++        DecType str typeParams types ->+            text "type" <+> text str <+> (hsep . map text $ typeParams) <+> text "=" <+>+                (hsep . punctuate (text " |") . map toDec $ types)++            where+                toDec (str, t) =+                    text str <+> (hsep . map vopParam $ t)
+ src/Elm/Expression.hs view
@@ -0,0 +1,112 @@+module Elm.Expression where++import Text.PrettyPrint hiding (Str)+import Data.Maybe++data Expr+    = App String [Expr]+    | Case Expr [(Expr, Expr)]+    | Let Expr [(Expr, Expr)] +    | List [Expr]+    | Tuple2 Expr Expr+    | Tuple3 Expr Expr Expr+    | Op String Expr Expr+    | Parens Expr+    | Str String+    | Int Int+    | Under+    | BoolTrue+    | BoolFalse+    | Record (Maybe Expr) [(String, Expr)]++var :: String -> Expr+var str = App str []++-- Takes an expression+-- if its a single variable or tuple then id+-- else wrap it in parens+vop :: Expr -> Doc+vop expr =+    case expr of+        App str [] ->+            text str++        Tuple2 exp1 exp2 ->+            toDoc $ Tuple2 exp1 exp2++        Tuple3 expr1 expr2 expr3 ->+            toDoc $ Tuple3 expr1 expr2 expr3++        Str str ->+            doubleQuotes $ text str++        Record a b ->+            toDoc $ Record a b++        other ->+            parens $ toDoc other++toDoc :: Expr -> Doc+toDoc expr =+    case expr of+        App str exprs ->+            text str <+> (hsep . map vop $ exprs)++        Tuple2 expr1 expr2 ->+            parens $ toDoc expr1 <> comma <+> toDoc expr2++        Tuple3 expr1 expr2 expr3 ->+            parens $ toDoc expr1 <> comma <+> toDoc expr2 <> comma <+> toDoc expr3++        Str str ->+            doubleQuotes . text $ str++        Op op expr1 expr2 ->+           vop expr1 <+> text op <+> vop expr2 ++        Case expr exprs ->+            hang (text "case" <+> vop expr <+> text "of") 4 (vcat . map caseToDoc $ exprs)++            where+                caseToDoc (expr1, expr2) =+                    toDoc expr1 <+> text "->" $$ (nest 4 $ toDoc expr2)+++        List exprs ->+            char '[' <> (hsep . punctuate (text ",") . map toDoc $ exprs) <> char ']'++        Let expr exprs ->+            text "let" $+$ (nest 4 . vcat . map letToDoc $ exprs) $+$ text "in" $+$ (nest 4 $ toDoc expr)+              +            where+                letToDoc (expr1, expr2) =+                    toDoc expr1 <+> char '=' <+> toDoc expr2++        Int i ->+            int i++        Under ->+            char '_'++        BoolTrue ->+            text "True"++        BoolFalse ->+            text "False"++        Record Nothing [] ->+            text "{}"++        Record (Just main) [] ->+            toDoc main++        Record main parts ->+            let+                front = fmap (\x -> toDoc x <+> char '|') main+            in+               char '{' <+> (Data.Maybe.fromMaybe empty front)+               <+> nest 4 (hsep . punctuate (char ',') . map docPart $ parts)+               <+> char '}'+            where+                docPart (name, value) =+                    text name <+> char '=' <+> toDoc value
+ src/Elm/Import.hs view
@@ -0,0 +1,55 @@+module Elm.Import where++import Text.PrettyPrint++data ImportType+    = Everything+    | Select [ImportItem]+    | ExposeNothing++data ImportItem +    = Item String+    | ItemExposing String [String]+    | ItemEvery String++data Import = Import String (Maybe String) ImportType++docItem :: ImportItem -> Doc+docItem item =+    case item of+        Item str ->+            text str++        ItemExposing name exposes ->+            text name <> (parens . hsep . punctuate (text ",") . map text $ exposes)++        ItemEvery name ->+            text name <> text "(..)"++exposingDoc :: ImportType -> Doc+exposingDoc importType =+    case importType of+        Everything ->+            text "exposing (..)"++        ExposeNothing ->+            empty++        Select imports ->+            text "exposing" <+> (parens . hsep . punctuate (text ",") . map docItem $ imports)++toDocI :: Import -> Doc+toDocI (Import name as exposing) =+    text "import" <+> text name <+> asDoc <+> exposingDoc exposing++    where+        asDoc =+            case as of+                Nothing ->+                    empty++                Just str ->+                    text "as" <+> text str+            +                +
+ src/Elm/Program.hs view
@@ -0,0 +1,28 @@+module Elm.Program where++import Elm.Import+import Elm.Decleration+import Text.PrettyPrint+import Data.String.Utils+import Data.List++data Program = Program String ImportType [Import] [Dec]++genProgram :: Program -> Doc+genProgram (Program name exports imports declerations) =+    text "module" <+> text name <+> exposingDoc exports +    $+$ (foldl ($+$) empty . map toDocI $ imports)+    $+$ (foldl ($+$) empty . map toDocD $ declerations)++renderProgram :: Program -> String+renderProgram program =+    let +        str = (render . genProgram $ program) ++ "\n"+    in+        join "\n" . map addNewline . split "\n" $ str+    where+        addNewline line =+            if (or $ map (\s -> isInfixOf s line) ["::", "type"]) then+                "\n" ++ line+            else+                line
+ src/Elm/Type.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE OverloadedStrings #-} +module Elm.Type where++import Elm.Expression+import Text.PrettyPrint+import Data.Maybe++data TypeDec+    = Params String [TypeDec]+    | TApp [TypeDec]+    | TTuple2 TypeDec TypeDec+    | TRecord (Maybe String) [(String, TypeDec)]++tvar :: String -> TypeDec+tvar str =+    Params str []++vopTApp :: TypeDec -> Doc+vopTApp t =+    case t of+        Params str types ->+            toDocT $ Params str types+            +        TRecord main decs ->+            toDocT $ TRecord main decs++        _ ->+            parens $ toDocT t++vopParam :: TypeDec -> Doc+vopParam t =+    case t of+        Params str [] ->+            text str++        _ ->+            parens $ toDocT t+        ++toDocT :: TypeDec -> Doc+toDocT t =+    case t of+        Params p decs ->+            text p <+> (hsep . map vopParam $ decs)++        TApp types ->+            hsep . punctuate (text " ->") . map vopTApp $ types+            +        TTuple2 t1 t2 ->+            lparen <> toDocT t1 <> comma <+> toDocT t2 <> rparen++        TRecord Nothing [] ->+            "{}"++        TRecord (Just main) [] ->+            text main++        TRecord main decs ->+            let+                front = fmap (\x -> text x <+> "|") main+            in+                "{" <+> Data.Maybe.fromMaybe empty front+                <+> (hsep . punctuate "," . map docDec $ decs)+                <+>  "}"+            where +                docDec (name, dec) =+                    text name <+> ":" <+> toDocT dec
− src/Expression.hs
@@ -1,112 +0,0 @@-module Expression where--import Text.PrettyPrint hiding (Str)-import Data.Maybe--data Expr-    = App String [Expr]-    | Case Expr [(Expr, Expr)]-    | Let Expr [(Expr, Expr)] -    | List [Expr]-    | Tuple2 Expr Expr-    | Tuple3 Expr Expr Expr-    | Op String Expr Expr-    | Parens Expr-    | Str String-    | Int Int-    | Under-    | BoolTrue-    | BoolFalse-    | Record (Maybe Expr) [(String, Expr)]--var :: String -> Expr-var str = App str []---- Takes an expression--- if its a single variable or tuple then id--- else wrap it in parens-vop :: Expr -> Doc-vop expr =-    case expr of-        App str [] ->-            text str--        Tuple2 exp1 exp2 ->-            toDoc $ Tuple2 exp1 exp2--        Tuple3 expr1 expr2 expr3 ->-            toDoc $ Tuple3 expr1 expr2 expr3--        Str str ->-            doubleQuotes $ text str--        Record a b ->-            toDoc $ Record a b--        other ->-            parens $ toDoc other--toDoc :: Expr -> Doc-toDoc expr =-    case expr of-        App str exprs ->-            text str <+> (hsep . map vop $ exprs)--        Tuple2 expr1 expr2 ->-            parens $ toDoc expr1 <> comma <+> toDoc expr2--        Tuple3 expr1 expr2 expr3 ->-            parens $ toDoc expr1 <> comma <+> toDoc expr2 <> comma <+> toDoc expr3--        Str str ->-            doubleQuotes . text $ str--        Op op expr1 expr2 ->-           vop expr1 <+> text op <+> vop expr2 --        Case expr exprs ->-            hang (text "case" <+> vop expr <+> text "of") 4 (vcat . map caseToDoc $ exprs)--            where-                caseToDoc (expr1, expr2) =-                    toDoc expr1 <+> text "->" $$ (nest 4 $ toDoc expr2)---        List exprs ->-            char '[' <> (hsep . punctuate (text ",") . map toDoc $ exprs) <> char ']'--        Let expr exprs ->-            text "let" $+$ (nest 4 . vcat . map letToDoc $ exprs) $+$ text "in" $+$ (nest 4 $ toDoc expr)-              -            where-                letToDoc (expr1, expr2) =-                    toDoc expr1 <+> char '=' <+> toDoc expr2--        Int i ->-            int i--        Under ->-            char '_'--        BoolTrue ->-            text "True"--        BoolFalse ->-            text "False"--        Record Nothing [] ->-            text "{}"--        Record (Just main) [] ->-            toDoc main--        Record main parts ->-            let-                front = fmap (\x -> toDoc x <+> char '|') main-            in-               char '{' <+> (Data.Maybe.fromMaybe empty front)-               <+> nest 4 (hsep . punctuate (char ',') . map docPart $ parts)-               <+> char '}'-            where-                docPart (name, value) =-                    text name <+> char '=' <+> toDoc value
− src/Import.hs
@@ -1,55 +0,0 @@-module Import where--import Text.PrettyPrint--data ImportType-    = Everything-    | Select [ImportItem]-    | ExposeNothing--data ImportItem -    = Item String-    | ItemExposing String [String]-    | ItemEvery String--data Import = Import String (Maybe String) ImportType--docItem :: ImportItem -> Doc-docItem item =-    case item of-        Item str ->-            text str--        ItemExposing name exposes ->-            text name <> (parens . hsep . punctuate (text ",") . map text $ exposes)--        ItemEvery name ->-            text name <> text "(..)"--exposingDoc :: ImportType -> Doc-exposingDoc importType =-    case importType of-        Everything ->-            text "exposing (..)"--        ExposeNothing ->-            empty--        Select imports ->-            text "exposing" <+> (parens . hsep . punctuate (text ",") . map docItem $ imports)--toDocI :: Import -> Doc-toDocI (Import name as exposing) =-    text "import" <+> text name <+> asDoc <+> exposingDoc exposing--    where-        asDoc =-            case as of-                Nothing ->-                    empty--                Just str ->-                    text "as" <+> text str-            -                -
− src/Program.hs
@@ -1,29 +0,0 @@-module Program where--import Import-import Decleration-import Text.PrettyPrint-import Data.String.Utils-import Text.Regex-import Data.List--data Program = Program String ImportType [Import] [Dec]--genProgram :: Program -> Doc-genProgram (Program name exports imports declerations) =-    text "module" <+> text name <+> exposingDoc exports -    $+$ (foldl ($+$) empty . map toDocI $ imports)-    $+$ (foldl ($+$) empty . map toDocD $ declerations)--renderProgram :: Program -> String-renderProgram program =-    let -        str = (render . genProgram $ program) ++ "\n"-    in-        join "\n" . map addNewline . split "\n" $ str-    where-        addNewline line =-            if (or $ map (\s -> isInfixOf s line) ["::", "type"]) then-                "\n" ++ line-            else-                line
− src/Type.hs
@@ -1,68 +0,0 @@-{-# LANGUAGE OverloadedStrings #-}--module Type where--import Expression-import Text.PrettyPrint-import Data.Maybe--data TypeDec-    = Params String [TypeDec]-    | TApp [TypeDec]-    | TTuple2 TypeDec TypeDec-    | TRecord (Maybe String) [(String, TypeDec)]--tvar :: String -> TypeDec-tvar str =-    Params str []--vopTApp :: TypeDec -> Doc-vopTApp t =-    case t of-        Params str types ->-            toDocT $ Params str types-            -        TRecord main decs ->-            toDocT $ TRecord main decs--        _ ->-            parens $ toDocT t--vopParam :: TypeDec -> Doc-vopParam t =-    case t of-        Params str [] ->-            text str--        _ ->-            parens $ toDocT t-        --toDocT :: TypeDec -> Doc-toDocT t =-    case t of-        Params p decs ->-            text p <+> (hsep . map vopParam $ decs)--        TApp types ->-            hsep . punctuate (text " ->") . map vopTApp $ types-            -        TTuple2 t1 t2 ->-            lparen <> toDocT t1 <> comma <+> toDocT t2 <> rparen--        TRecord Nothing [] ->-            "{}"--        TRecord (Just main) [] ->-            text main--        TRecord main decs ->-            let-                front = fmap (\x -> text x <+> "|") main-            in-                "{" <+> Data.Maybe.fromMaybe empty front-                <+> (hsep . punctuate "," . map docDec $ decs)-                <+>  "}"-            where -                docDec (name, dec) =-                    text name <+> ":" <+> toDocT dec
test/Spec.hs view
@@ -3,14 +3,13 @@ module Main where  import Test.HUnit-import Expression-import Type-import Decleration-import Import-import Program+import Elm.Expression+import Elm.Type+import Elm.Decleration+import Elm.Import+import Elm.Program import Text.PrettyPrint hiding (Str) import Control.Monad -import Data.String.Utils  assertString :: String -> String -> String -> Assertion  assertString preface expected actual =