Skip to content

Commit

Permalink
feat: support async iterable<> (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz authored and Marcos Cáceres committed Aug 7, 2019
1 parent 76dca6b commit cf8caf5
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 14 deletions.
18 changes: 13 additions & 5 deletions lib/productions/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ export class IterableLike extends Base {
const tokens = {};
const ret = new IterableLike({ source: tokeniser.source, tokens });
tokens.readonly = tokeniser.consume("readonly");
tokens.base = tokens.readonly ?
tokeniser.consume("maplike", "setlike") :
if (!tokens.readonly) {
tokens.async = tokeniser.consume("async");
}
tokens.base =
tokens.readonly ? tokeniser.consume("maplike", "setlike") :
tokens.async ? tokeniser.consume("iterable") :
tokeniser.consume("iterable", "maplike", "setlike");
if (!tokens.base) {
tokeniser.unconsume(start_position);
return;
}

const { type } = ret;
const secondTypeRequired = type === "maplike";
const secondTypeRequired = type === "maplike" || ret.async;
const secondTypeAllowed = secondTypeRequired || type === "iterable";

tokens.open = tokeniser.consume("<") || tokeniser.error(`Missing less-than sign \`<\` in ${type} declaration`);
Expand All @@ -30,8 +34,9 @@ export class IterableLike extends Base {
if (first.tokens.separator) {
ret.idlType.push(type_with_extended_attributes(tokeniser));
}
else if (secondTypeRequired)
tokeniser.error(`Missing second type argument in ${type} declaration`);
else if (secondTypeRequired) {
tokeniser.error(`Missing second type argument in ${type} declaration`);
}
}
tokens.close = tokeniser.consume(">") || tokeniser.error(`Missing greater-than sign \`>\` in ${type} declaration`);
tokens.termination = tokeniser.consume(";") || tokeniser.error(`Missing semicolon after ${type} declaration`);
Expand All @@ -45,4 +50,7 @@ export class IterableLike extends Base {
get readonly() {
return !!this.tokens.readonly;
}
get async() {
return !!this.tokens.async;
}
}
1 change: 1 addition & 0 deletions lib/tokeniser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const nonRegexTerminals = [
"Infinity",
"NaN",
"Promise",
"async",
"boolean",
"byte",
"double",
Expand Down
1 change: 1 addition & 0 deletions lib/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export function write(ast, { templates: ts = templates } = {}) {
return ts.definition(ts.wrap([
extended_attributes(it.extAttrs),
token(it.tokens.readonly),
token(it.tokens.async),
token(it.tokens.base, ts.generic),
token(it.tokens.open),
ts.wrap(it.idlType.map(type)),
Expand Down
3 changes: 3 additions & 0 deletions test/invalid/baseline/async-iterable-readonly.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Syntax error at line 3 in async-iterable-readonly.webidl, since `interface AsyncIterable`:
readonly async iterable<long
^ Missing return type
3 changes: 3 additions & 0 deletions test/invalid/baseline/async-iterable-single.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Syntax error at line 3 in async-iterable-single.webidl, since `interface AsyncIterable`:
async iterable<long>;
^ Missing second type argument in iterable declaration
3 changes: 3 additions & 0 deletions test/invalid/baseline/async-maplike.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Syntax error at line 3 in async-maplike.webidl, since `interface AsyncIterable`:
async maplike<DOMString,
^ Missing return type
4 changes: 4 additions & 0 deletions test/invalid/idl/async-iterable-readonly.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Exposed=Window]
interface AsyncIterable {
readonly async iterable<long, short>;
};
4 changes: 4 additions & 0 deletions test/invalid/idl/async-iterable-single.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Exposed=Window]
interface AsyncIterable {
async iterable<long>;
};
4 changes: 4 additions & 0 deletions test/invalid/idl/async-maplike.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Exposed=Window]
interface AsyncIterable {
async maplike<DOMString, DOMString>;
};
87 changes: 87 additions & 0 deletions test/syntax/baseline/async-iterable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[
{
"type": "interface",
"name": "AsyncIterable",
"inheritance": null,
"members": [
{
"type": "iterable",
"idlType": [
{
"type": null,
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "long"
},
{
"type": null,
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "float"
}
],
"extAttrs": [],
"readonly": false,
"async": true
}
],
"extAttrs": [],
"partial": false
},
{
"type": "interface",
"name": "AsyncIterableWithExtAttr",
"inheritance": null,
"members": [
{
"type": "iterable",
"idlType": [
{
"type": null,
"extAttrs": [
{
"type": "extended-attribute",
"name": "XAttr2",
"rhs": null,
"arguments": []
}
],
"generic": "",
"nullable": false,
"union": false,
"idlType": "DOMString"
},
{
"type": null,
"extAttrs": [
{
"type": "extended-attribute",
"name": "XAttr3",
"rhs": null,
"arguments": []
}
],
"generic": "",
"nullable": false,
"union": false,
"idlType": "long"
}
],
"extAttrs": [],
"readonly": false,
"async": true
}
],
"extAttrs": [],
"partial": false
},
{
"type": "eof",
"value": "",
"trivia": "\n"
}
]
9 changes: 6 additions & 3 deletions test/syntax/baseline/iterable.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down Expand Up @@ -49,7 +50,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down Expand Up @@ -80,7 +82,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down
9 changes: 6 additions & 3 deletions test/syntax/baseline/maplike.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down Expand Up @@ -57,7 +58,8 @@
}
],
"extAttrs": [],
"readonly": true
"readonly": true,
"async": false
}
],
"extAttrs": [],
Expand Down Expand Up @@ -103,7 +105,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down
9 changes: 6 additions & 3 deletions test/syntax/baseline/setlike.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand All @@ -41,7 +42,8 @@
}
],
"extAttrs": [],
"readonly": true
"readonly": true,
"async": false
}
],
"extAttrs": [],
Expand Down Expand Up @@ -72,7 +74,8 @@
}
],
"extAttrs": [],
"readonly": false
"readonly": false,
"async": false
}
],
"extAttrs": [],
Expand Down
7 changes: 7 additions & 0 deletions test/syntax/idl/async-iterable.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface AsyncIterable {
async iterable<long, float>;
};

interface AsyncIterableWithExtAttr {
async iterable<[XAttr2] DOMString, [XAttr3] long>;
};

0 comments on commit cf8caf5

Please sign in to comment.