0 | module Text.WebIDL.Types.Attribute
3 | import Derive.Prelude
4 | import Language.Reflection.Util
6 | import Text.WebIDL.Types.Numbers
7 | import Text.WebIDL.Types.StringLit
8 | import Text.WebIDL.Types.Identifier
9 | import Text.WebIDL.Types.Symbol
12 | %language ElabReflection
15 | isParenOrQuote : Char -> Bool
16 | isParenOrQuote '(' = True
17 | isParenOrQuote ')' = True
18 | isParenOrQuote '[' = True
19 | isParenOrQuote ']' = True
20 | isParenOrQuote '{' = True
21 | isParenOrQuote '}' = True
22 | isParenOrQuote '"' = True
23 | isParenOrQuote _ = False
26 | isCommaOrParenOrQuote : Char -> Bool
27 | isCommaOrParenOrQuote ',' = True
28 | isCommaOrParenOrQuote c = isParenOrQuote c
32 | Other = NS I [IntLit,FloatLit,StringLit,Identifier,Keyword,Symbol]
36 | data EAInner : Type where
43 | (::) : (head : Either EAInner Other) -> (eai : EAInner) -> EAInner
45 | %runElab derive "EAInner" [Eq,Show]
48 | 0 InnerOrOther : Type
49 | InnerOrOther = Either EAInner Other
52 | innerAttribute : SnocList InnerOrOther -> EAInner -> EAInner
53 | innerAttribute [<] x = x
54 | innerAttribute (sx :< y) x = innerAttribute sx $
y :: x
60 | size : EAInner -> Nat
61 | size (Left eai :: t) = size eai + size t
62 | size (Right _ :: t) = 1 + size t
67 | leaves : EAInner -> Nat
68 | leaves (Left eai :: t) = leaves eai + leaves t
69 | leaves (Right _ :: t) = 1 + leaves t
74 | depth : EAInner -> Nat
75 | depth (Left eai :: t) = 1 + (depth eai `max` depth t)
76 | depth (Right _ :: t) = 1 + depth t
85 | data ExtAttribute : Type where
90 | Last : InnerOrOther -> ExtAttribute
91 | Cons : InnerOrOther -> ExtAttribute -> ExtAttribute
93 | %runElab derive "ExtAttribute" [Eq,Show]
96 | extAttribute : SnocList InnerOrOther -> ExtAttribute -> ExtAttribute
97 | extAttribute [<] x = x
98 | extAttribute (sx :< y) x = extAttribute sx $
Cons y x
100 | namespace ExtAttribute
104 | size : ExtAttribute -> Nat
105 | size (Cons (Left i) r) = size i + size r
106 | size (Cons (Right _) r) = 1 + size r
107 | size (Last (Left i)) = size i
108 | size (Last (Right _)) = 1
112 | leaves : ExtAttribute -> Nat
113 | leaves (Cons (Left i) r) = leaves i + leaves r
114 | leaves (Cons (Right _) r) = 1 + leaves r
115 | leaves (Last (Left i)) = leaves i + 1
116 | leaves (Last (Right _)) = 2
120 | depth : ExtAttribute -> Nat
121 | depth (Cons (Left i) r) = 1 + (depth i `max` depth r)
122 | depth (Cons (Right _) r) = 1 + depth r
123 | depth (Last (Left i)) = 1 + depth i
124 | depth (Last (Right _)) = 1
135 | ExtAttributeList : Type
136 | ExtAttributeList = List ExtAttribute
141 | Attributed : Type -> Type
142 | Attributed a = (ExtAttributeList, a)
145 | interface HasAttributes a where
146 | constructor MkHasAttributes
147 | attributes : a -> ExtAttributeList
150 | HasAttributes () where
151 | attributes = const Nil
154 | HasAttributes String where
155 | attributes = const Nil
157 | public export %inline
158 | HasAttributes Identifier where
159 | attributes = const Nil
161 | public export %inline
162 | HasAttributes Bool where
163 | attributes = const Nil
165 | public export %inline
166 | HasAttributes FloatLit where
167 | attributes = const Nil
169 | public export %inline
170 | HasAttributes IntLit where
171 | attributes = const Nil
173 | public export %inline
174 | HasAttributes StringLit where
175 | attributes = const Nil
177 | public export %inline
178 | (HasAttributes a, HasAttributes b) => HasAttributes (a,b) where
179 | attributes (x,y) = attributes x ++ attributes y
181 | public export %inline
182 | HasAttributes ExtAttribute where
185 | public export %inline
186 | HasAttributes a => HasAttributes (Maybe a) where
187 | attributes = maybe Nil attributes
189 | public export %inline
190 | HasAttributes a => HasAttributes (List a) where
191 | attributes x = x >>= attributes
193 | public export %inline
194 | HasAttributes a => HasAttributes (List1 a) where
195 | attributes = attributes . forget
202 | (all : NP HasAttributes ts) => HasAttributes (NP I ts) where
203 | attributes = hcconcatMap HasAttributes attributes
206 | (all : NP HasAttributes ts) => HasAttributes (NS I ts) where
207 | attributes = hcconcatMap HasAttributes attributes
210 | (all : POP HasAttributes ts) => HasAttributes (SOP I ts) where
211 | attributes = hcconcatMap HasAttributes attributes
221 | generalAttrType : (implicits : List Arg) -> (arg : TTImp) -> TTImp
222 | generalAttrType is arg = piAll `(~(arg) -> ExtAttributeList
) is
227 | attrClaim : (fun : Name) -> (p : ParamTypeInfo) -> Decl
229 | let arg := p.applied
230 | tpe := generalAttrType (allImplicits p "HasAttributes") arg
236 | attrImplClaim : (impl : Name) -> (p : ParamTypeInfo) -> Decl
237 | attrImplClaim impl p = implClaim impl (implType "HasAttributes" p)
245 | attrImplDef : (fun, impl : Name) -> Decl
247 | def i [patClause (var i) (var "MkHasAttributes" `app` var f)]
249 | parameters (nms : List Name)
250 | ttimp : BoundArg 1 Regular -> TTImp
251 | ttimp (BA (MkArg _ _ _ t) [x] _) = assertIfRec nms t `(attributes
~(var x))
253 | rsh : SnocList TTImp -> TTImp
255 | rsh st = `(listBind
~(listOf st) id
)
258 | attrClauses : (fun : Name) -> TypeInfo -> List Clause
259 | attrClauses fun ti = map clause ti.cons
262 | clause : Con ti.arty ti.args -> Clause
264 | let ns := freshNames "x" c.arty
266 | lhs := var fun `app` bc
267 | st := ttimp <$> boundArgs regular c.args [ns]
268 | in patClause lhs (rsh st)
271 | attrDef : Name -> TypeInfo -> Decl
272 | attrDef fun ti = def fun (attrClauses fun ti)
283 | HasAttributes : List Name -> ParamTypeInfo -> Res (List TopLevel)
284 | HasAttributes nms p =
285 | let fun := funName p "attributes"
286 | impl := implName p "HasAttributes"
288 | [ TL (attrClaim fun p) (attrDef nms fun p.info)
289 | , TL (attrImplClaim impl p) (attrImplDef fun impl)
296 | isParenTrue : all Attribute.isParenOrQuote (unpack "(){}[]\"") = True
299 | isParenFalse : any Attribute.isParenOrQuote (unpack "=!?><:;,.-_") = False
300 | isParenFalse = Refl
302 | isCommaOrParenTrue : all Attribute.isCommaOrParenOrQuote (unpack ",(){}[]\"") = True
303 | isCommaOrParenTrue = Refl
305 | isCommaOrParenFalse : any Attribute.isCommaOrParenOrQuote (unpack "=!?><:;.-_") = False
306 | isCommaOrParenFalse = Refl