Skip to content

Commit

Permalink
[ style ] adhere to style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoeck committed Aug 23, 2023
1 parent 8087ea4 commit 742492d
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 361 deletions.
90 changes: 50 additions & 40 deletions src/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ setOutDir : String -> Config -> Either (List String) Config
setOutDir s = Right . { outDir := s }

descs : List $ OptDescr (Config -> Either (List String) Config)
descs = [ MkOpt ['o'] ["outDir"] (ReqArg setOutDir "<dir>")
"output directory"
]
descs =
[ MkOpt
['o']
["outDir"]
(ReqArg setOutDir "<dir>")
"output directory"
]

applyArgs : List String -> Either (List String) Config
applyArgs args =
case getOpt RequireOrder descs args of
MkResult opts n [] [] => foldl (>>=) (Right $ init n) opts
MkResult _ _ u e => Left $ map unknown u ++ e
MkResult opts n [] [] => foldl (>>=) (Right $ init n) opts
MkResult _ _ u e => Left $ map unknown u ++ e

where unknown : String -> String
unknown = ("Unknown option: " ++)
where
unknown : String -> String
unknown = ("Unknown option: " ++)

--------------------------------------------------------------------------------
-- Codegen
Expand All @@ -65,52 +70,57 @@ runProg (MkEitherT p) = do

fromCodegen : Codegen a -> Prog a
fromCodegen = toProgWith (fastUnlines . map err) . pure
where err : CodegenErr -> String
err (CBInterfaceInvalidOps x y k) =
"Invalid number of callback operations in \{x.domain}: \{y.value} (\{show k} operations)"
err (RegularOpWithoutName x y) =
"Unnamed regular operation in \{x.domain}: \{y.value}"
err (InvalidGetter x y) =
"Invalid getter in \{x.domain}: \{y.value}"
err (InvalidSetter x y) =
"Invalid setter in \{x.domain}: \{y.value}"
err (UnresolvedAlias x y) =
"Unresolved alias in \{x.domain}: \{y.value}"
err (AnyInUnion x) = "\"Any\" type in a union type in \{x.domain}"
err (PromiseInUnion x) = "\"Promise\" type in a union type in \{x.domain}"
err (NullableAny x) = "Nullable \"Any\" type in \{x.domain}"
err (NullablePromise x) = "Nullable \"Promise\" type in \{x.domain}"
err (InvalidConstType x) = "Invalid constant type in \{x.domain}"
where
err : CodegenErr -> String
err (CBInterfaceInvalidOps x y k) =
"Invalid number of callback operations in \{x.domain}: \{y.value} (\{show k} operations)"
err (RegularOpWithoutName x y) =
"Unnamed regular operation in \{x.domain}: \{y.value}"
err (InvalidGetter x y) =
"Invalid getter in \{x.domain}: \{y.value}"
err (InvalidSetter x y) =
"Invalid setter in \{x.domain}: \{y.value}"
err (UnresolvedAlias x y) =
"Unresolved alias in \{x.domain}: \{y.value}"
err (AnyInUnion x) = "\"Any\" type in a union type in \{x.domain}"
err (PromiseInUnion x) = "\"Promise\" type in a union type in \{x.domain}"
err (NullableAny x) = "Nullable \"Any\" type in \{x.domain}"
err (NullablePromise x) = "Nullable \"Promise\" type in \{x.domain}"
err (InvalidConstType x) = "Invalid constant type in \{x.domain}"

writeDoc : String -> String -> Prog ()
writeDoc f doc = toProg $ writeFile f doc

covering
loadDef : String -> Prog (String,PartsAndDefs)
loadDef f = let mn = moduleName
. head
. split ('.' ==)
. last
$ split ('/' ==) f

in do s <- toProg (readFile f)
d <- toProg (pure $ parseIdl partsAndDefs s)
pure (mn,d)
loadDef f =
let mn :=
moduleName
. head
. split ('.' ==)
. last
$ split ('/' ==) f

in do
s <- toProg (readFile f)
d <- toProg (pure $ parseIdl partsAndDefs s)
pure (mn,d)

typesGen : Config -> List CGDomain -> Prog ()
typesGen c ds =
let typesFile = c.outDir ++ "/Web/Internal/Types.idr"
let typesFile := c.outDir ++ "/Web/Internal/Types.idr"
in writeDoc typesFile (typedefs ds)

codegen : Config -> CGDomain -> Prog ()
codegen c d =
let typesFile = c.outDir ++ "/Web/Internal/" ++ d.name ++ "Types.idr"
primFile = c.outDir ++ "/Web/Internal/" ++ d.name ++ "Prim.idr"
apiFile = c.outDir ++ "/Web/Raw/" ++ d.name ++ ".idr"
let typesFile := c.outDir ++ "/Web/Internal/" ++ d.name ++ "Types.idr"
primFile := c.outDir ++ "/Web/Internal/" ++ d.name ++ "Prim.idr"
apiFile := c.outDir ++ "/Web/Raw/" ++ d.name ++ ".idr"

in do writeDoc typesFile (types d)
writeDoc primFile (primitives d)
writeDoc apiFile (definitions d)
in do
writeDoc typesFile (types d)
writeDoc primFile (primitives d)
writeDoc apiFile (definitions d)

logAttributes : HasAttributes a => a -> Prog ()
logAttributes = traverse_ (putStrLn . extAttribute) . attributes
Expand All @@ -125,7 +135,7 @@ run args = do
config <- toProg (pure $ applyArgs args)
ds <- toDomains <$> traverse loadDef config.files

let e = env config.maxInheritance ds
let e := env config.maxInheritance ds

doms <- fromCodegen (traverse (domain e) ds)

Expand Down
58 changes: 33 additions & 25 deletions src/Text/WebIDL/Codegen/Args.idr
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,14 @@ parameters {opts : LayoutOpts}
funType : (name : IdrisIdent) -> ReturnType -> Args -> Doc opts
funType n t as =
typeDecl n (returnTypeAPI t) (run 0 as [<] [<] [<])

where
run : Nat -> Args -> (imp,aut,expl : SnocList $ Doc opts) -> List (Doc opts)
run _ [] is aus es = is <>> aus <>> es <>> []
run k (a::as) is aus es = case CGArg.inheritance a of
Just (n,_) =>
let k2 := S k
pk2 := "t\{show k2}"
let k2 := S k
pk2 := "t\{show k2}"
impl := line "{auto 0 _ : JSType \{pk2}}"
aut := line "{auto 0 _ : Elem \{n} (Types \{pk2})}"
expl = arg (prettyArgAPI k2 a)
Expand All @@ -254,12 +255,13 @@ callbackFFI :
callbackFFI o n impl as t =
let cbTpe := functionTypeOnly (returnTypeFFI' "IO" t) (map prettyArgFFI as)
retTpe := line "PrimIO \{o}"
in render80 . indent 2 $ vsep
[ line ""
, line "export"
, line "\{impl}"
, typeDecl n retTpe [cbTpe]
]
in render80 . indent 2 $
vsep
[ line ""
, line "export"
, line "\{impl}"
, typeDecl n retTpe [cbTpe]
]

export
callbackAPI :
Expand All @@ -270,18 +272,21 @@ callbackAPI :
-> (tpe : ReturnType)
-> String
callbackAPI o n prim as t =
let cbTpe = functionTypeOnly (returnTypeFFI' "IO" t)
(map prettyArgFFI as)

retTpe = line "JSIO \{o}"
impl = line "\{n} cb = primJS $ \{prim} cb"

in render80 . indent 2 $ vsep
[ line ""
, line "export"
, typeDecl n retTpe [cbTpe]
, impl
]
let cbTpe :=
functionTypeOnly
(returnTypeFFI' "IO" t)
(map prettyArgFFI as)

retTpe := line "JSIO \{o}"
impl := line "\{n} cb = primJS $ \{prim} cb"

in render80 . indent 2 $
vsep
[ line ""
, line "export"
, typeDecl n retTpe [cbTpe]
, impl
]

export
funFFI :
Expand Down Expand Up @@ -313,9 +318,10 @@ fun' ns name prim as us rt =

primNS := "\{kindToString ns}.\{prim}"

primCall := if sameType rt
then "primJS"
else "tryJS " ++ namespacedIdent ns name
primCall :=
if sameType rt
then "primJS"
else "tryJS " ++ namespacedIdent ns name

lhs := unwords $ "\{name}" :: vs

Expand Down Expand Up @@ -365,8 +371,10 @@ fun ns name prim as t =
funImpl := fun' ns mainName prim as [] t

-- function without optional args
funImpl2 = if null undefs then []
else fun' ns name prim as2 undefs t
funImpl2 :=
if null undefs
then []
else fun' ns name prim as2 undefs t

in render80 . indent 2 $ vsep (funImpl ++ funImpl2)

Expand Down
57 changes: 35 additions & 22 deletions src/Text/WebIDL/Codegen/Definitions.idr
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ typeImports = "import JS"
--------------------------------------------------------------------------------

extern : CGDomain -> String
extern d = fastUnlines
[ section "Interfaces" $ exts ext name d.ifaces
, section "Dictionaries" $ exts extNoCast name d.dicts
, section "Mixins" $ exts extNoCast name d.mixins
, section "Callbacks" $ exts extNoCast name d.callbacks
]
extern d =
fastUnlines
[ section "Interfaces" $ exts ext name d.ifaces
, section "Dictionaries" $ exts extNoCast name d.dicts
, section "Mixins" $ exts extNoCast name d.mixins
, section "Callbacks" $ exts extNoCast name d.callbacks
]

where
extNoCast : String -> String
extNoCast s = """
Expand All @@ -57,10 +59,11 @@ extern d = fastUnlines
safeCast = unsafeCastOnPrototypeName "\{s}"
"""

exts : (f : String -> String)
-> (a -> Identifier)
-> List a
-> List String
exts :
(f : String -> String)
-> (a -> Identifier)
-> List a
-> List String
exts f g = map (("\n" ++) . f) . sort . map (value . g)

--------------------------------------------------------------------------------
Expand All @@ -86,8 +89,8 @@ primCallbacks = cbacks (pure . primCallback)

jsTypes : List CGDomain -> String
jsTypes ds =
let ifs = sortBy (comparing name) (ds >>= ifaces)
dics = sortBy (comparing name) (ds >>= dicts)
let ifs := sortBy (comparing name) (ds >>= ifaces)
dics := sortBy (comparing name) (ds >>= dicts)
in section "Inheritance" $
map (\i => jsType i.name i.super) ifs ++
map (\d => jsType d.name d.super) dics
Expand All @@ -98,8 +101,10 @@ jsTypes ds =

ifaces' : (CGIface -> List String) -> CGDomain -> String
ifaces' f = section "Interfaces" . map ns . sortBy (comparing name) . ifaces
where ns : CGIface -> String
ns i = namespaced i.name (f i)

where
ns : CGIface -> String
ns i = namespaced i.name (f i)

ifaces : CGDomain -> String
ifaces = ifaces' $ \(MkIface n s cs fs) => constants cs ++ functions fs
Expand All @@ -113,8 +118,10 @@ primIfaces = ifaces' (primFunctions . functions)

dicts' : (CGDict -> List String) -> CGDomain -> String
dicts' f = section "Dictionaries" . map ns . sortBy (comparing name) . dicts
where ns : CGDict -> String
ns d = namespaced d.name (f d)

where
ns : CGDict -> String
ns d = namespaced d.name (f d)

dicts : CGDomain -> String
dicts = dicts' $ \(MkDict n s fs) => functions fs
Expand All @@ -128,8 +135,10 @@ primDicts = dicts' (primFunctions . functions)

mixins' : (CGMixin -> List String) -> CGDomain -> String
mixins' f = section "Mixins" . map ns . sortBy (comparing name) . mixins
where ns : CGMixin -> String
ns m = namespaced m.name (f m)

where
ns : CGMixin -> String
ns m = namespaced m.name (f m)

mixins : CGDomain -> String
mixins = mixins' $ \(MkMixin n cs fs) => constants cs ++ functions fs
Expand All @@ -143,7 +152,8 @@ primMixins = mixins' (primFunctions . functions)

export
typedefs : List CGDomain -> String
typedefs ds = """
typedefs ds =
"""
module Web.Internal.Types
import JS
Expand Down Expand Up @@ -179,7 +189,8 @@ typedefs ds = """
--
export
types : CGDomain -> String
types d = """
types d =
"""
module Web.Internal.\{d.name}Types
\{typeImports}
Expand All @@ -192,7 +203,8 @@ types d = """

export
primitives : CGDomain -> String
primitives d = """
primitives d =
"""
module Web.Internal.\{d.name}Prim
import JS
Expand All @@ -208,7 +220,8 @@ primitives d = """

export
definitions : CGDomain -> String
definitions d = """
definitions d =
"""
module Web.Raw.\{d.name}
\{defImports d}
Expand Down
Loading

0 comments on commit 742492d

Please sign in to comment.