Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use actual data constructor name

Do not assume the data constructor's name is the type's name.
  • Loading branch information...
commit ccd24c894734231115c777e823a8b64972bc3a63 1 parent 7b89f4a
@singpolyma authored
Showing with 12 additions and 10 deletions.
  1. +5 −4 ParseRecords.hs
  2. +7 −6 mustache2hs.hs
View
9 ParseRecords.hs
@@ -9,8 +9,8 @@ import Language.Haskell.Syntax
import Data.Text (Text)
import qualified Data.Text as T
-type Records = [(String, Record)]
-type Record = [Field]
+type Records = [(String, Record)] -- Typename, data constructor
+type Record = (String, [Field]) -- Data constructor name, fields
type Field = (Text, MuType)
data MuType = MuList String | MuLambda | MuVariable deriving (Show, Eq)
@@ -47,8 +47,9 @@ extractFromField :: [(String, HsType)] -> ([HsName], HsBangType) -> Field
extractFromField types (name, t) =
(T.pack $ concatMap hsNameToString name, hsTypeToMuType types $ extractTypeFromBangType t)
-extractFromRecordConstructor :: [(String, HsType)] -> HsConDecl -> [Field]
-extractFromRecordConstructor types (HsRecDecl _ _ fields) = map (extractFromField types) fields
+extractFromRecordConstructor :: [(String, HsType)] -> HsConDecl -> Record
+extractFromRecordConstructor types (HsRecDecl _ cname fields) =
+ (hsNameToString cname, map (extractFromField types) fields)
extractFromRecordConstructor _ _ = error "Only single data-constructor records may be used as context"
extractFromDataDecl :: [(String, HsType)] -> HsDecl -> (String, Record)
View
13 mustache2hs.hs
@@ -167,13 +167,13 @@ codeGenTree fname rname recs tree = do
recordMustExist (Just r) = r
recordMustExist _ = error ("No record named: " ++ rname)
pattern rec = mconcat [
- Builder.fromString rname,
+ Builder.fromString (fst rec),
Builder.fromString " {",
mintercalate comma $ map (\x -> mconcat [
Builder.fromText x,
Builder.fromString "=",
Builder.fromText x
- ]) (map fst rec),
+ ]) (map fst $ snd rec),
Builder.fromString "}"
]
wsep = Builder.fromString "; "
@@ -185,9 +185,10 @@ codeGen _ _ (MuText txt) = return (mconcat [
Builder.fromShow (T.unpack txt)
], [], [])
codeGen _ _ (MuVar name False) = return (mconcat [
- Builder.fromString "fromMaybe mempty (Builder.fromText $ toPathPiece",
+ Builder.fromString "fromMaybe mempty (fmap ",
+ Builder.fromString "(Builder.fromText . toPathPiece) (",
Builder.fromText name,
- Builder.fromString ")"
+ Builder.fromString "))"
], [], [])
codeGen _ _ (MuVar name True) = return (mconcat [
Builder.fromString "fromMaybe mempty (fmap ",
@@ -196,7 +197,7 @@ codeGen _ _ (MuVar name True) = return (mconcat [
Builder.fromString "))"
], [], [])
codeGen (rname,rec) recs (MuSection name stree)
- | lookup name rec == Just MuLambda =
+ | lookup name (snd rec) == Just MuLambda =
return (mconcat [
Builder.fromText name,
Builder.fromString " (",
@@ -208,7 +209,7 @@ codeGen (rname,rec) recs (MuSection name stree)
id <- get
modify succ
let nm = name `mappend` T.pack (show id)
- case lookup name rec of
+ case lookup name (snd rec) of
Just (MuList rname) -> do
(helper, partials) <- codeGenTree nm rname recs stree
return (mconcat [
Please sign in to comment.
Something went wrong with that request. Please try again.