-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdomconv-cps.hs
690 lines (543 loc) · 23.1 KB
/
domconv-cps.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
-- DOM interface converter: a tool to convert Haskell files produced by
-- H/Direct into properly structured DOM wrapper
-- CPS-style interface generator
module Main where
import Prelude hiding (putStrLn)
import System.Environment.UTF8
import System.Directory
import System.FilePath
import System.Exit
import System.IO (stderr, stdin, openFile, IOMode (..))
import System.IO.UTF8
import Control.Monad
import Data.Maybe
import Data.Either
import Data.List
import Data.Char
import Language.Haskell.Pretty
import Language.Preprocessor.Cpphs
import qualified Language.Haskell.Syntax as H
import qualified Data.Map as M
import qualified Data.Set as S
import qualified OmgParser
import LexM
import qualified IDLSyn as I
import IDLUtils
import Literal
import BasicTypes
import JS.Jcode
import JS.Show
import SplitBounds
main = do
args <- getArgs
let epopts = parseOptions args
case epopts of
Left s -> do
hPutStrLn stderr $ "domconv: command line parse error " ++ s
exitWith (ExitFailure 1)
Right opts -> procopts opts
procopts opts = do
let cppfiles = infiles opts
(hsrc, inclfile) =
case cppfiles of
[] -> (hGetContents stdin, "<stdin>")
["-"] -> (hGetContents stdin, "<stdin>")
(a:_) -> (openFile a ReadMode >>= hGetContents, a)
let baseopt = [("boolean", "Bool")]
optsb = opts {defines = ((defines opts) ++ baseopt)
,boolopts = ((boolopts opts) {pragma = True}) }
hsrc' <- hsrc
hsrcpr <- runCpphs optsb inclfile hsrc'
x <- runLexM [] inclfile hsrcpr OmgParser.parseIDL
let showMod (I.Module i d) = do
putStrLn $ "module" ++ show i
mapM (putStrLn . show) d
return ()
showMod _ = return ()
putStrLn "{--"
mapM showMod x
putStrLn "--}"
let prntmap = mkParentMap x
-- mapM (putStrLn . show) (M.assocs prntmap)
let valmsg = valParentMap prntmap
when (length valmsg > 0) $ do
mapM_ (hPutStrLn stderr) valmsg
exitWith (ExitFailure 2)
let modst = DOMState {
pm = prntmap
,imp = []
,ns = ""
,modpref = ""
,procmod = []
,convlog = []
}
modst' = domLoop modst x
mapM_ (hPutStrLn stderr) (convlog modst')
let splitmod = splitModule (head $ procmod modst')
mapM_ putSplit splitmod
-- Retrieve a module name as a string from Module
modName :: H.Module -> String
modName m = read $ drop 1 $ dropWhile (not . isSpace) (show m)
-- Get a module namespace (all elements of name separated with dots except
-- the last one)
modNS :: String -> String
modNS mn = concat $ intersperse "." mnpts where
mnpts = case (reverse $ parts (== '.') mn) of
[] -> []
[_] -> []
(p:ps) -> reverse ps
-- Write a module surrounded by split begin/end comments
putSplit :: H.HsModule -> IO ()
putSplit mod@(H.HsModule _ modid _ _ _) = do
putStrLn $ "\n" ++ splitBegin ++ "/" ++ (modName modid) ++ "\n"
putStrLn $ prettyPrint mod
putStrLn $ "\n" ++ splitEnd ++ "\n"
-- Split a proto-module created by domLoop. All class, data, and instance definitions
-- remain in the "head" class. All methods are grouped by their `this' argument
-- context and placed into modules with the name of that context (first character removed).
-- All modules get the same imports that the "head" module has plus the "head" module itself.
splitModule :: H.HsModule -> [H.HsModule]
splitModule (H.HsModule _ modid mbexp imps decls) = headmod : submods where
headns = modNS $ modName modid
headmod = H.HsModule nullLoc modid headexp imps headdecls
headdecls = datas ++ classes ++ instances
headexp = Just $ map (mkEIdent . declname) (datas ++ classes)
datas = filter datadecl decls
datadecl (H.HsDataDecl _ _ _ _ _ _) = True
datadecl (H.HsNewTypeDecl _ _ _ _ _ _) = True
datadecl _ = False
classes = filter classdecl decls
classdecl (H.HsClassDecl _ _ _ _ _) = True
classdecl _ = False
instances = filter instdecl decls
instdecl (H.HsInstDecl _ _ _ _ _) = True
instdecl _ = False
expname (H.HsEVar (H.UnQual (H.HsIdent s))) = s
expname _ = ""
declname (H.HsDataDecl _ _ (H.HsIdent s) _ _ _) = s
declname (H.HsNewTypeDecl _ _ (H.HsIdent s) _ _ _) = s
declname (H.HsClassDecl _ _ (H.HsIdent s) _ _) = s
declname (H.HsTypeSig _ [H.HsIdent s] _) = s
declname (H.HsFunBind [H.HsMatch _ (H.HsIdent s) _ _ _]) = s
declname _ = ""
mtsigs = filter methtsig (reverse decls)
methtsig (H.HsTypeSig _ _ _) = True
methtsig (H.HsFunBind _) = True
methtsig _ = False
corrn = drop 1 . dropWhile (/= '|')
methcorrn (H.HsTypeSig x [H.HsIdent s] y) = H.HsTypeSig x [H.HsIdent (corrn s)] y
methcorrn (H.HsFunBind [H.HsMatch x (H.HsIdent s) y z t]) =
H.HsFunBind [H.HsMatch x (H.HsIdent (corrn s)) y z t]
methcorrn z = z
methassoc meth =
let i = ns ++ takeWhile (/= '|') (declname meth)
ns = case headns of
"" -> ""
mns -> mns ++ "."
in (i, methcorrn meth)
methmap = mkmethmap M.empty (map methassoc mtsigs)
mkmethmap m [] = m
mkmethmap m ((i, meth) : ims) = mkmethmap addmeth ims where
addmeth = case M.lookup i m of
Nothing -> M.insert i [meth] m
(Just meths) -> M.insert i (meth : meths) m
submods = M.elems $ M.mapWithKey mksubmod methmap
mksubmod iid smdecls =
H.HsModule nullLoc (H.Module iid) (Just subexp)
(mkModImport modid : (imps ++ docimp)) smdecls where
subexp = map mkEIdent $ nub $ filter (not . isSuffixOf "'") $ map declname smdecls
docimp = case "createElement" `elem` (map declname smdecls) of
True -> []
_ -> [(mkModImport (H.Module docmod))
{H.importSpecs = Just (False, [H.HsIVar $ H.HsIdent "createElement"])}] where
docmod = concat $ intersperse "." $ reverse
("Document" : tail (reverse $ parts (== '.') iid))
-- Loop through the list of toplevel parse results (modules, pragmas).
-- Pragmas modify state, modules don't.
domLoop :: DOMState -> [I.Defn] -> DOMState
domLoop st [] = st
domLoop st (def : defs) = case def of
I.Pragma prgm -> domLoop (prgm2State st (dropWhile isSpace prgm)) defs
I.Module id moddef ->
let prmod = mod2mod st (I.Module id' moddef)
modn = ns st ++ (renameMod $ concat $ intersperse "." $
reverse $ parts ( == '.') (getDef def ++ modpref st))
id' = I.Id modn
imp' = modn : imp st
modl = prmod : (procmod st) in
domLoop st {procmod = modl, imp = imp'} defs
z ->
let logmsg = "Expected a Module or a Pragma; found " ++ (show z) in
domLoop st {convlog = convlog st ++ [logmsg]} defs
-- Modify DOMState based on a pragma encountered
prgm2State :: DOMState -> String -> DOMState
{--
prgm2State st ('p':'r':'e':'f':'i':'x':pref) =
let prefst = read (dropWhile isSpace pref)
dot = if length prefst == 0 then "" else "." in
st {modpref = dot ++ prefst}
--}
prgm2State st ('n':'a':'m':'e':'s':'p':'a':'c':'e':nns) =
let nnsst = read (dropWhile isSpace nns)
dot = if length nnsst == 0 then "" else "." in
st {ns = nnsst ++ dot}
prgm2State st upgm =
let logmsg = "Unknown pragma " ++ upgm in
st {convlog = convlog st ++ [logmsg]}
-- Validate a map of interface inheritance. Any "Left" parent identifier
-- causes a log message to be produced. It is also checked that an interface
-- does not have itself as a parent. Circular inheritance is not checked.
valParentMap :: M.Map String [Either String String] -> [String]
valParentMap pm = concat (M.elems m2) where
m2 = M.mapWithKey lefts pm
lefts intf parents = concat $ map (leftmsg intf) parents
leftmsg intf (Right _) = []
leftmsg intf (Left p) = ["Interface " ++ intf ++ " has " ++ p ++ " as a parent, but " ++
p ++ " is not defined anywhere"]
-- Prepare a complete map of interfaces inheritance. All ancestors
-- must be defined in the IDL module being processed plus in other
-- modules it includes.
mkParentMap :: [I.Defn] -> M.Map String [Either String String]
mkParentMap defns = m2 where
allintfs = nub $ concat $ map getintfs defns
getintfs (I.Module _ moddefs) = filter intfOnly moddefs
getintfs _ = []
m1 = M.fromList $ zip (map getDef allintfs) allintfs
m2 = M.fromList (map getparents allintfs)
getparents i@(I.Interface _ supers _) = (getDef i, concat $ map parent supers)
parent pidf = case (pidf `M.member` m1) of
True -> (Right pidf) : snd (getparents (fromJust $ M.lookup pidf m1))
False -> [Left pidf]
-- Fake source location
nullLoc = H.SrcLoc {H.srcFilename = "", H.srcLine = 0, H.srcColumn = 0}
-- A list of single-letter formal argument names (max. 26)
azList = map (: []) ['a' .. 'z']
azHIList = map H.HsIdent azList
-- Rename a module. First character of module name is uppercased. Each
-- underscore followed by a character causes that character uppercased.
renameMod :: String -> String
renameMod "" = ""
renameMod (m:odule) = toUpper m : renameMod' odule where
renameMod' "" = ""
renameMod' ('_':o:dule) = '.' : toUpper o : renameMod' dule
renameMod' ('.':o:dule) = '.' : toUpper o : renameMod' dule
renameMod' (o:dule) = o : renameMod' dule
-- Module converter mutable state (kind of)
data DOMState = DOMState {
pm :: M.Map String [Either String String] -- inheritance map
,imp :: [String] -- import list
,ns :: String -- output module namespace (#pragma namespace)
,modpref :: String -- module name prefix (#pragma prefix)
,procmod :: [H.HsModule] -- modules already processed
,convlog :: [String] -- conversion messages
} deriving (Show)
-- Helpers to produce class and datatype identifiers out of DOM identifiers
classFor s = "C" ++ s
typeFor s = "T" ++ s
-- Convert an IDL module definition into Haskell module syntax
mod2mod :: DOMState -> I.Defn -> H.HsModule
mod2mod st md@(I.Module _ moddefs) =
H.HsModule nullLoc (H.Module modid') (Just []) imps decls where
modid' = renameMod $ getDef md
imps = map mkModImport (map H.Module (["CPS" ,"UnsafeJS"] ++ imp st))
intfs = filter intfOnly moddefs
eqop op1 op2 = getDef op1 == getDef op2
decls = types ++ classes ++ instances ++ methods ++ attrs ++ makers
makers = concat $ map intf2maker intfs
classes = concat $ map intf2class intfs
methods = concat $ map intf2meth intfs
types = concat $ map intf2type intfs
attrs = concat $ map intf2attr intfs
instances = concat $ map (intf2inst $ pm st) intfs
mod2mod _ z = error $ "Input of mod2mod should be a Module but is " ++ show z
-- Create a module import declaration
mkModImport :: H.Module -> H.HsImportDecl
mkModImport s = H.HsImportDecl {H.importLoc = nullLoc
,H.importQualified = False
,H.importModule = s
,H.importAs = Nothing
,H.importSpecs = Nothing}
-- For each interface, locate it in the inheritance map,
-- and produce instance declarations for the corresponding datatype.
intf2inst :: M.Map String [Either String String] -> I.Defn -> [H.HsDecl]
intf2inst pm intf@(I.Interface _ _ _) = self : parents where
sid = getDef intf
self = mkInstDecl sid sid
parents = case M.lookup sid pm of
Nothing -> []
Just ess -> map (flip mkInstDecl sid) (map (either id id) ess)
intf2inst _ _ = []
-- For each interface found, locate all constants it contains
intf2const :: I.Defn -> [H.HsDecl]
intf2const intf@(I.Interface _ _ defs) =
map oneconst (filter constOnly defs)
-- Produce a Haskell nullary function declaration out of a constant
oneconst :: I.Defn -> H.HsDecl
oneconst cnst@(I.Constant (I.Id cid) _ ctyp (I.Lit (IntegerLit clit))) =
let match = H.HsMatch nullLoc (H.HsIdent cid) []
(H.HsUnGuardedRhs (H.HsLit (H.HsString (show clit)))) []
in H.HsFunBind [match]
-- For each interface found, define a newtype with the same name
intf2type :: I.Defn -> [H.HsDecl]
intf2type intf@(I.Interface _ _ _) =
let typename = H.HsIdent (typeFor $ getDef intf) in
[H.HsDataDecl nullLoc [] typename []
[H.HsConDecl nullLoc typename []] []]
intf2type _ = []
-- Convert an Interface specification into a class specification
intf2class :: I.Defn -> [H.HsDecl]
intf2class intf@(I.Interface _ supers _) =
[H.HsClassDecl nullLoc sups (H.HsIdent (classFor $ getDef intf)) (take 1 azHIList) []] where
sups = map name2ctxt supers
intf2class _ = []
-- Convert a name to a type context assertion (assume single parameter class)
name2ctxt name = (mkUIdent $ classFor name, [H.HsTyVar $ head azHIList])
-- A helper function to produce an unqualified identifier
mkUIdent = H.UnQual . H.HsIdent
-- A filter to select only operations (methods)
opsOnly :: I.Defn -> Bool
opsOnly (I.Operation _ _ _ _ _) = True
opsOnly _ = False
-- A filter to select only attributes
attrOnly :: I.Defn -> Bool
attrOnly (I.Attribute _ _ _ _ _) = True
attrOnly _ = False
-- A filter to select only interfaces (classes)
intfOnly :: I.Defn -> Bool
intfOnly (I.Interface _ _ cldefs) = True
intfOnly _ = False
-- A filter to select only constant definitions
constOnly :: I.Defn -> Bool
constOnly (I.Constant _ _ _ _) = True
constOnly _ = False
-- Collect all operations defined in an interface
collectOps :: I.Defn -> [I.Defn]
collectOps (I.Interface _ _ cldefs) =
filter opsOnly cldefs
collectOps _ = []
-- Collect all constants defined in an interface
collectConst :: I.Defn -> [I.Defn]
collectConst (I.Interface _ _ cldefs) =
filter constOnly cldefs
collectConst _ = []
-- Collect all attributes defined in an interface
collectAttrs :: I.Defn -> [I.Defn]
collectAttrs (I.Interface _ _ cldefs) =
filter attrOnly cldefs
collectAttrs _ = []
-- Declare an instance (very simple case, no context, no methods only one class parameter)
mkInstDecl :: String -> String -> H.HsDecl
mkInstDecl clname typename =
H.HsInstDecl nullLoc [] (mkUIdent $ classFor clname) [mkTIdent $ typeFor typename] []
-- For certain interfaces (ancestors of HTMLElement), special maker functions
-- are introduced to simplify creation of the formers.
intf2maker :: I.Defn -> [H.HsDecl]
intf2maker intf@(I.Interface (I.Id iid) _ _) =
case (tagFor iid) of
"" -> []
tag -> [mktsig, mkimpl] where
mkimpl =
let defmaker = iid ++ "|mk" ++ renameMod tag
flipv = H.HsVar (mkUIdent "flip")
crelv = H.HsVar (mkUIdent "createElement")
tagv = H.HsLit (H.HsString tag)
rhs = H.HsUnGuardedRhs (H.HsApp flipv (H.HsApp crelv tagv))
match = H.HsMatch nullLoc (H.HsIdent defmaker) [] rhs [] in
H.HsFunBind [match]
mktsig =
let cpstv = mkTIdent "CPS c"
defmaker = iid ++ "|mk" ++ renameMod tag
parms = [H.HsIdent "a"]
actx = (mkUIdent (classFor "HTMLDocument"),[mkTIdent "a"])
tpsig = mkTsig parms (H.HsTyApp cpstv (mkTIdent (typeFor iid)))
retts = H.HsQualType (actx : []) tpsig in
H.HsTypeSig nullLoc [H.HsIdent defmaker] retts
intf2maker _ = []
-- Tag values corresponding to certain HTML element interfaces
{--
tagFor "HTMLButtonElement" = "button"
tagFor "HTMLDivElement" = "div"
tagFor "HTMLImageElement" = "img"
tagFor "HTMLAppletElement" = "applet"
tagFor "HTMLFontElement" = "font"
tagFor "HTMLFormElement" = "form"
tagFor "HTMLFrameElement" = "frame"
tagFor "HTMLInputElement" = "input"
tagFor "HTMLObjectElement" = "object"
tagFor "HTMLParagraphElement" = "p"
tagFor "HTMLParamElement" = "param"
tagFor "HTMLPreElement" = "pre"
tagFor "HTMLScriptElement" = "script"
tagFor "HTMLTableCellElement" = "td"
tagFor "HTMLTableColElement" = "col"
tagFor "HTMLTableElement" = "table"
tagFor "HTMLTableRowElement" = "tr"
tagFor "HTMLTextAreaElement" = "textarea"
tagFor "HTMLBRElement" = "br"
tagFor "HTMLHRElement" = "hr"
tagFor "HTMLLIElement" = "li"
tagFor "HTMLDListElement" = "dl"
tagFor "HTMLOListElement" = "ol"
tagFor "HTMLUListElement" = "ul"
tagFor "HTMLSpanElement" = "span"
--}
tagFor "HTMLParagraphElement" = "p"
tagFor "HTMLTableCellElement" = "td"
tagFor "HTMLDListElement" = "dl"
tagFor "HTMLOListElement" = "ol"
tagFor "HTMLUListElement" = "ul"
tagFor "HTMLTableRowElement" = "tr"
tagFor "HTMLTableCaptionElement" = "caption"
tagFor "HTMLImageElement" = "img"
tagFor "HTMLTableSectionElement" = ""
tagFor "HTMLAnchorElement" = "a"
tagFor ('H':'T':'M':'L':s) =
let suff = "Element"
in if isSuffixOf suff s
then map toLower (take (length s - length suff) s)
else ""
tagFor _ = ""
-- Attributes are represented by methods with proper type signatures.
-- These methods are wrappers around type-neutral unsafe get/set property
-- functions.
intf2attr :: I.Defn -> [H.HsDecl]
intf2attr intf@(I.Interface (I.Id iid) _ cldefs) =
concat $ map mkattr $ collectAttrs intf where
mkattr (I.Attribute [] _ _ _ _) = []
mkattr (I.Attribute [I.Id iat] False tat _ _) = mksetter iid iat tat ++ mkgetter iid iat tat
mkattr (I.Attribute [I.Id iat] True tat _ _) = mkgetter iid iat tat
mkattr (I.Attribute (iatt:iats) b tat raises ext) =
mkattr (I.Attribute [iatt] b tat raises ext) ++ mkattr (I.Attribute iats b tat raises ext)
mksetter iid iat tat = [stsig iid iat tat, simpl iid iat]
simpl iid iat =
let defset = iid ++ "|set'" ++ iat
unssetp = H.HsVar (mkUIdent "unsafeSetProperty")
propnam = H.HsLit (H.HsString iat)
rhs = H.HsUnGuardedRhs (H.HsApp unssetp propnam)
match = H.HsMatch nullLoc (H.HsIdent defset) [] rhs [] in
H.HsFunBind [match]
stsig iid iat tat =
let cpstv = mkTIdent "CPS c"
ityp = I.TyName iid Nothing
defset = iid ++ "|set'" ++ iat
parm = [I.Param (I.Id "val") tat [I.Mode In]]
parms = (map (fst . tyParm) parm) ++ [H.HsIdent "zz"]
contxt = (concat $ map (snd . tyParm) parm) ++ ctxRet ityp
tpsig = mkTsig parms (H.HsTyApp cpstv (tyRet ityp))
retts = H.HsQualType contxt tpsig in
H.HsTypeSig nullLoc [H.HsIdent defset] retts
mkgetter iid iat tat = [gtsig iid iat tat, gimpl iid iat]
gimpl iid iat =
let defget = iid ++ "|get'" ++ iat
unsgetp = H.HsVar (mkUIdent "unsafeGetProperty")
propnam = H.HsLit (H.HsString iat)
rhs = H.HsUnGuardedRhs (H.HsApp unsgetp propnam)
match = H.HsMatch nullLoc (H.HsIdent defget) [] rhs [] in
H.HsFunBind [match]
gtsig iid iat tat =
let cpstv = mkTIdent "CPS c"
defget = iid ++ "|get'" ++ iat
parms = [H.HsIdent "this"]
thisctx = (mkUIdent (classFor iid),[mkTIdent "this"])
tpsig = mkTsig parms (H.HsTyApp cpstv (tyRet tat))
retts = H.HsQualType (thisctx : ctxRet tat) tpsig in
H.HsTypeSig nullLoc [H.HsIdent defget] retts
intf2attr _ = []
-- Methods are lifted to top level. Declared argument types are converted
-- into type constraints unless they are of primitive types. First argument
-- always gets a type of the interface where the method is declared.
-- Only `In' parameters are supported at this time.
-- Constants are converted into nullary methods always returning their values
intf2meth :: I.Defn -> [H.HsDecl]
intf2meth intf@(I.Interface _ _ cldefs) =
(concat $ map mkmeth $ collectOps intf) ++
(concat $ map mkconst $ collectConst intf) where
getDefHs op = getDef op
getDefJs op@(I.Operation _ _ _ mbctx _) = case mbctx of
Nothing -> getDef op
Just [] -> getDef op
Just (s:_) -> s
mkconst cn@(I.Constant (I.Id cid) _ _ (I.Lit (IntegerLit (ILit base val)))) =
let defcn = getDef intf ++ "|c" ++ cid
match = H.HsMatch nullLoc (H.HsIdent defcn) [] crhs []
crhs = H.HsUnGuardedRhs (H.HsLit (H.HsInt val))
in [H.HsFunBind [match]]
mkmeth op = tsig op : timpl op
tsig op@(I.Operation (I.FunId _ _ parm) optype _ _ _) =
let cpstv = mkTIdent "CPS c"
defop = getDef intf ++ "|" ++ getDefHs op
parms = (H.HsIdent "this") : (map (fst . tyParm) parm)
contxt = (concat $ map (snd . tyParm) parm) ++ ctxRet optype
thisctx = (mkUIdent (classFor $ getDef intf),[mkTIdent "this"])
tpsig = mkTsig parms (H.HsTyApp cpstv (tyRet optype))
retts = H.HsQualType (thisctx : contxt) tpsig in
H.HsTypeSig nullLoc [H.HsIdent defop] retts
timpl op@(I.Operation (I.FunId _ _ parm) optype _ _ _) =
let defop = getDef intf ++ "|" ++ getDefHs op
defop' = defop ++ "'"
tocpsev = H.HsVar (mkUIdent "toCPE")
parms = map H.HsPVar (take (1 + length parm) azHIList)
parmv = map (H.HsVar . H.UnQual)
(H.HsIdent (getDefHs op ++ "'") : take (1 + length parm) azHIList)
mkApp [] = error "Application with empty list"
mkApp [a] = a
mkApp (a:as) = H.HsApp a (mkApp as)
rhs' = H.HsUnGuardedRhs (H.HsApp (H.HsVar (mkUIdent "unsafeJS"))
(H.HsLit (H.HsString mbody)))
mbody = strJexp (mkMethBody (getDefJs op) (take (1 + length parm) azList)) ++ ";"
rhs = H.HsUnGuardedRhs (mkApp [tocpsev, H.HsParen $ mkApp parmv])
match = H.HsMatch nullLoc (H.HsIdent defop) parms rhs []
match' = H.HsMatch nullLoc (H.HsIdent defop') parms rhs' [] in
[H.HsFunBind [match], H.HsFunBind [match']]
intf2meth _ = []
-- Create a Javascript body for a method
mkMethBody :: String -> [String] -> Jexp
mkMethBody _ [] = error $ "A method should have at least one argument"
mkMethBody meth args =
let (this:rgs) = map (\p -> JCall (JStr "exprEval") [JStr p]) args
body = JMethod meth this rgs in
JCall (JStr "return") [body]
-- Build a method's type signature
mkTsig :: [H.HsName] -> H.HsType -> H.HsType
mkTsig [] a = a
mkTsig (p:ps) a = H.HsTyFun (H.HsTyVar p) (mkTsig ps a)
-- A helper function to produce a type identifier
mkTIdent = H.HsTyVar . H.HsIdent
-- A helper function to produce an export identifier
mkEIdent = H.HsEVar . H.UnQual . H.HsIdent
-- Obtain a return type signature from a return type
tyRet :: I.Type -> H.HsType
tyRet (I.TyName c Nothing) = case (asIs c) of
Nothing -> mkTIdent "zz"
Just c' -> mkTIdent c'
tyRet (I.TyInteger _) = mkTIdent "Int"
tyRet (I.TyApply _ (I.TyInteger _)) = mkTIdent "Int"
tyRet (I.TyFloat _) = mkTIdent "Float"
tyRet (I.TyApply _ (I.TyFloat _)) = mkTIdent "Float"
tyRet I.TyVoid = H.HsTyTuple []
tyRet t = error $ "Return type " ++ (show t)
-- Obtain a return type context (if any) from a return type
ctxRet :: I.Type -> [H.HsAsst]
ctxRet (I.TyName c Nothing) = case (asIs c) of
Nothing -> [(mkUIdent $ classFor c, [mkTIdent "zz"])]
Just c' -> []
ctxRet _ = []
-- Obtain a type signature from a parameter definition
tyParm :: I.Param -> (H.HsName, [H.HsAsst])
tyParm (I.Param (I.Id p) ptype [I.Mode In]) =
let hsidp = H.HsIdent p in
case ptype of
I.TyName c Nothing -> case asIs c of
Just cc -> (H.HsIdent cc, [])
Nothing -> (hsidp, [(mkUIdent $ classFor c, [mkTIdent p])])
I.TyInteger _ -> (H.HsIdent "Int", [])
I.TyFloat _ -> (H.HsIdent "Float", [])
I.TyApply _ (I.TyInteger _) -> (H.HsIdent "Int", [])
I.TyApply _ (I.TyFloat _) -> (H.HsIdent "Float", [])
t -> error $ "Param type " ++ (show t)
tyParm (I.Param _ _ _) = error "Unsupported parameter attributes"
-- Some types pass through as is, other are class names
asIs :: String -> Maybe String
asIs "DOMString" = Just "String"
asIs "Bool" = Just "Bool"
asIs _ = Nothing