@@ -7,6 +7,8 @@ import type {
77 WebIdl ,
88 Method ,
99 Typed ,
10+ Dictionary ,
11+ Member ,
1012} from "./types.js" ;
1113import { readdir , readFile } from "fs/promises" ;
1214import { merge } from "./helpers.js" ;
@@ -82,6 +84,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
8284 const enums : Record < string , Enum > = { } ;
8385 const mixin : Record < string , DeepPartial < Interface > > = { } ;
8486 const interfaces : Record < string , DeepPartial < Interface > > = { } ;
87+ const dictionary : Record < string , DeepPartial < Dictionary > > = { } ;
8588
8689 for ( const node of nodes ) {
8790 const name = string ( node . values [ 0 ] ) ;
@@ -95,6 +98,9 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
9598 case "interface" :
9699 interfaces [ name ] = handleMixinandInterfaces ( node , "interface" ) ;
97100 break ;
101+ case "dictionary" :
102+ dictionary [ name ] = handleDictionary ( node ) ;
103+ break ;
98104 default :
99105 throw new Error ( `Unknown node name: ${ node . name } ` ) ;
100106 }
@@ -104,6 +110,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
104110 enums : { enum : enums } ,
105111 mixins : { mixin } ,
106112 interfaces : { interface : interfaces } ,
113+ dictionaries : { dictionary } ,
107114 } ;
108115}
109116
@@ -268,6 +275,45 @@ function handleMethod(child: Node): Partial<Method> {
268275 return { name, signature } ;
269276}
270277
278+ /**
279+ * Handles dictionary nodes
280+ * @param child The dictionary node to handle.
281+ */
282+ function handleDictionary ( child : Node ) : DeepPartial < Dictionary > {
283+ const name = string ( child . values [ 0 ] ) ;
284+ const member : Record < string , Partial < Member > > = { } ;
285+
286+ for ( const c of child . children ) {
287+ switch ( c . name ) {
288+ case "member" : {
289+ const memberName = string ( c . values [ 0 ] ) ;
290+ member [ memberName ] = handleMember ( c ) ;
291+ break ;
292+ }
293+ default :
294+ throw new Error ( `Unknown node name: ${ c . name } ` ) ;
295+ }
296+ }
297+
298+ return {
299+ name,
300+ members : { member } ,
301+ } ;
302+ }
303+
304+ /**
305+ * Handles dictionary member nodes
306+ * @param c The member node to handle.
307+ */
308+ function handleMember ( c : Node ) : Partial < Member > {
309+ const name = string ( c . values [ 0 ] ) ;
310+ return {
311+ name,
312+ ...optionalMember ( "type" , "string" , c . properties ?. type ) ,
313+ ...optionalMember ( "required" , "boolean" , c . properties ?. required ) ,
314+ } ;
315+ }
316+
271317/**
272318 * Collect all file URLs in a directory.
273319 */
0 commit comments