From f4292a7a21008b80c1a2ee17acd7d825d1fec5dc Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Tue, 5 Jun 2018 13:10:49 +0900 Subject: [PATCH] Generics always as an array (#188) * remove unused opt * generics always as an array * refactor: iterables always have subtype arrays * refactor: inline modifiers --- lib/webidl2.js | 4 +- lib/writer.js | 31 ++------ test/syntax/json/dictionary.json | 18 +++-- test/syntax/json/generic.json | 114 ++++++++++++++++------------- test/syntax/json/promise-void.json | 18 +++-- test/syntax/json/record.json | 52 ++++++------- test/syntax/json/sequence.json | 68 +++++++++-------- test/syntax/json/typedef.json | 18 +++-- test/syntax/json/typesuffixes.json | 18 +++-- 9 files changed, 177 insertions(+), 164 deletions(-) diff --git a/lib/webidl2.js b/lib/webidl2.js index 42178165..10c210b8 100644 --- a/lib/webidl2.js +++ b/lib/webidl2.js @@ -321,11 +321,11 @@ switch (name.type) { case "Promise": if (probe("[")) error("Promise type cannot have extended attribute"); - ret.idlType = return_type(typeName); + ret.idlType = [return_type(typeName)]; break; case "sequence": case "FrozenArray": - ret.idlType = type_with_extended_attributes(typeName); + ret.idlType = [type_with_extended_attributes(typeName)]; break; case "record": if (probe("[")) error("Record key cannot have extended attribute"); diff --git a/lib/writer.js b/lib/writer.js index ac4778a9..aeebe5bd 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -1,27 +1,13 @@ "use strict"; (() => { - function write(ast, opt = {}) { - const noop = str => str; - const optNames = "type".split(" "); - const context = []; - for (const o of optNames) { - if (!opt[o]) opt[o] = noop; - } - - function literal(it) { - return it.value; - }; + function write(ast) { function type(it) { - if (typeof it === "string") return opt.type(it); // XXX should maintain some context + if (typeof it === "string") return it; let ret = extended_attributes(it.extAttrs); if (it.union) ret += `(${it.idlType.map(type).join(" or ")})`; - else { - if (it.generic) ret += `${it.generic}<`; - if (Array.isArray(it.idlType)) ret += it.idlType.map(type).join(", "); - else ret += type(it.idlType); - if (it.generic) ret += ">"; - } + else if (it.generic) ret += `${it.generic}<${it.idlType.map(type).join(", ")}>`; + else ret += type(it.idlType); if (it.nullable) ret += "?"; return ret; @@ -46,14 +32,12 @@ return ret; }; function make_ext_at(it) { - context.unshift(it); let ret = it.name; if (it.rhs) { if (it.rhs.type === "identifier-list") ret += `=(${it.rhs.value.join(",")})`; else ret += `=${it.rhs.value}`; } if (it.arguments) ret += `(${it.arguments.length ? it.arguments.map(argument).join(",") : ""})`; - context.shift(); // XXX need to add more contexts, but not more than needed for ReSpec return ret; }; function extended_attributes(eats) { @@ -61,11 +45,10 @@ return `[${eats.map(make_ext_at).join(", ")}]`; }; - const modifiers = "getter setter deleter stringifier static".split(" "); function operation(it) { let ret = extended_attributes(it.extAttrs); if (it.stringifier && !it.idlType) return "stringifier;"; - for (const mod of modifiers) { + for (const mod of ["getter", "setter", "deleter", "stringifier", "static"]) { if (it[mod]) ret += mod + " "; } ret += type(it.idlType) + " "; @@ -155,10 +138,10 @@ return ret + "};"; }; function iterable(it) { - return `iterable<${Array.isArray(it.idlType) ? it.idlType.map(type).join(", ") : type(it.idlType)}>;`; + return `iterable<${it.idlType.map(type).join(", ")}>;`; }; function legacyiterable(it) { - return `legacyiterable<${Array.isArray(it.idlType) ? it.idlType.map(type).join(", ") : type(it.idlType)}>;`; + return `legacyiterable<${it.idlType.map(type).join(", ")}>;`; }; function maplike(it) { return `${it.readonly ? "readonly " : ""}maplike<${it.idlType.map(type).join(", ")}>;`; diff --git a/test/syntax/json/dictionary.json b/test/syntax/json/dictionary.json index 8bbc6b9d..b7f28d2a 100644 --- a/test/syntax/json/dictionary.json +++ b/test/syntax/json/dictionary.json @@ -67,14 +67,16 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "dictionary-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "long", - "extAttrs": [] - }, + "idlType": [ + { + "type": "dictionary-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "long", + "extAttrs": [] + } + ], "extAttrs": [] }, "extAttrs": [], diff --git a/test/syntax/json/generic.json b/test/syntax/json/generic.json index 29331444..e817028e 100644 --- a/test/syntax/json/generic.json +++ b/test/syntax/json/generic.json @@ -16,28 +16,34 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": "Promise", - "nullable": false, - "union": false, - "idlType": { + "idlType": [ + { "type": "return-type", - "generic": "sequence", + "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": true, - "union": false, - "idlType": "DOMString", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": "sequence", + "nullable": false, + "union": false, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": true, + "union": false, + "idlType": "DOMString", + "extAttrs": [] + } + ], + "extAttrs": [] + } + ], "extAttrs": [] - }, - "extAttrs": [] - }, + } + ], "extAttrs": [] }, "name": "bar", @@ -56,14 +62,16 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "attribute-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "DOMString", - "extAttrs": [] - }, + "idlType": [ + { + "type": "attribute-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "DOMString", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "baz", @@ -98,14 +106,16 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": true, - "union": false, - "idlType": "Client", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": true, + "union": false, + "idlType": "Client", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "getServiced", @@ -125,14 +135,16 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "any", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "any", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "reloadAll", @@ -168,14 +180,16 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "any", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "any", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "default", diff --git a/test/syntax/json/promise-void.json b/test/syntax/json/promise-void.json index 6340f8d8..e071f226 100644 --- a/test/syntax/json/promise-void.json +++ b/test/syntax/json/promise-void.json @@ -15,14 +15,16 @@ "generic": "Promise", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "void", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "void", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "meow", diff --git a/test/syntax/json/record.json b/test/syntax/json/record.json index 1a8aa1b1..9657aaf3 100644 --- a/test/syntax/json/record.json +++ b/test/syntax/json/record.json @@ -32,31 +32,33 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "argument-type", - "generic": "record", - "nullable": false, - "union": false, - "idlType": [ - { - "type": "argument-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "ByteString", - "extAttrs": [] - }, - { - "type": "argument-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "any", - "extAttrs": [] - } - ], - "extAttrs": [] - }, + "idlType": [ + { + "type": "argument-type", + "generic": "record", + "nullable": false, + "union": false, + "idlType": [ + { + "type": "argument-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "ByteString", + "extAttrs": [] + }, + { + "type": "argument-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "any", + "extAttrs": [] + } + ], + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "param", diff --git a/test/syntax/json/sequence.json b/test/syntax/json/sequence.json index 20f74a3f..cf8333be 100644 --- a/test/syntax/json/sequence.json +++ b/test/syntax/json/sequence.json @@ -32,14 +32,16 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "argument-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "float", - "extAttrs": [] - }, + "idlType": [ + { + "type": "argument-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "float", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "coordinates", @@ -60,14 +62,16 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "return-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "float", - "extAttrs": [] - }, + "idlType": [ + { + "type": "return-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "float", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "getInflectionPoints", @@ -119,21 +123,23 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "argument-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "long", - "extAttrs": [ - { - "name": "XAttr", - "arguments": null, - "type": "extended-attribute", - "rhs": null - } - ] - }, + "idlType": [ + { + "type": "argument-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "long", + "extAttrs": [ + { + "name": "XAttr", + "arguments": null, + "type": "extended-attribute", + "rhs": null + } + ] + } + ], "extAttrs": [] }, "name": "arg", diff --git a/test/syntax/json/typedef.json b/test/syntax/json/typedef.json index 2fc815d0..e6ecf3a0 100644 --- a/test/syntax/json/typedef.json +++ b/test/syntax/json/typedef.json @@ -58,14 +58,16 @@ "generic": "sequence", "nullable": false, "union": false, - "idlType": { - "type": "typedef-type", - "generic": null, - "nullable": false, - "union": false, - "idlType": "Point", - "extAttrs": [] - }, + "idlType": [ + { + "type": "typedef-type", + "generic": null, + "nullable": false, + "union": false, + "idlType": "Point", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "PointSequence", diff --git a/test/syntax/json/typesuffixes.json b/test/syntax/json/typesuffixes.json index 47d2a98c..c5fb9329 100644 --- a/test/syntax/json/typesuffixes.json +++ b/test/syntax/json/typesuffixes.json @@ -32,14 +32,16 @@ "generic": "sequence", "nullable": true, "union": false, - "idlType": { - "type": "argument-type", - "generic": null, - "nullable": true, - "union": false, - "idlType": "DOMString", - "extAttrs": [] - }, + "idlType": [ + { + "type": "argument-type", + "generic": null, + "nullable": true, + "union": false, + "idlType": "DOMString", + "extAttrs": [] + } + ], "extAttrs": [] }, "name": "foo",