Skip to content

Commit

Permalink
Generics always as an array (#188)
Browse files Browse the repository at this point in the history
* remove unused opt

* generics always as an array

* refactor: iterables always have subtype arrays

* refactor: inline modifiers
  • Loading branch information
saschanaz authored Jun 5, 2018
1 parent 1ea9f9c commit f4292a7
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 164 deletions.
4 changes: 2 additions & 2 deletions lib/webidl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
31 changes: 7 additions & 24 deletions lib/writer.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -46,26 +32,23 @@
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) {
if (!eats || !eats.length) return "";
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) + " ";
Expand Down Expand Up @@ -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(", ")}>;`;
Expand Down
18 changes: 10 additions & 8 deletions test/syntax/json/dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
114 changes: 64 additions & 50 deletions test/syntax/json/generic.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 10 additions & 8 deletions test/syntax/json/promise-void.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 27 additions & 25 deletions test/syntax/json/record.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit f4292a7

Please sign in to comment.