Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse required dictionary fields. #25

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ A dictionary looks like this:
{
"type": "field",
"name": "fillPattern",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand Down Expand Up @@ -255,6 +256,7 @@ All the members are fields as follows:

* `type`: Always "field".
* `name`: The name of the field.
* `required`: Boolean indicating whether this is a [required](https://heycam.github.io/webidl/#required-dictionary-member) field.
* `idlType`: An [IDL Type](#idl-type) describing what field's type.
* `extAttrs`: A list of [extended attributes](#extended-attributes).
* `default`: A [default value](#default-and-const-values), absent if there is none.
Expand Down
6 changes: 5 additions & 1 deletion lib/webidl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,19 @@
}
var ea = extended_attrs(store ? mems : null);
all_ws(store ? mems : null, "pea");
var required = consume(ID, "required");
var typ = type() || error("No type for dictionary member");
all_ws();
var name = consume(ID) || error("No name for dictionary member");
var dflt = default_();
if (required && dflt) error("Required member must not have a default");
ret.members.push({
type: "field"
, name: name.value
, required: !!required
, idlType: typ
, extAttrs: ea
, "default": default_()
, "default": dflt
});
all_ws();
consume(OTHER, ";") || error("Unterminated dictionary member");
Expand Down
5 changes: 5 additions & 0 deletions test/invalid/idl/dict-required-default.widl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://heycam.github.io/webidl/#required-dictionary-member
// "A required dictionary member must not have a default value."
dictionary Dict {
required long member = 0;
};
4 changes: 4 additions & 0 deletions test/invalid/json/dict-required-default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "Required member must not have a default"
, "line": 4
}
2 changes: 2 additions & 0 deletions test/syntax/idl/dictionary.widl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dictionary PaintOptions {
Point position;
// https://heycam.github.io/webidl/#dfn-optional-argument-default-value allows sequences to default to "[]".
sequence<long> seq = [];
// https://heycam.github.io/webidl/#required-dictionary-member
required long reqSeq;
};

partial dictionary A {
Expand Down
4 changes: 4 additions & 0 deletions test/syntax/json/dictionary-inherits.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "field",
"name": "fillPattern",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -24,6 +25,7 @@
{
"type": "field",
"name": "strokePattern",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -40,6 +42,7 @@
{
"type": "field",
"name": "position",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -62,6 +65,7 @@
{
"type": "field",
"name": "hydrometry",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand Down
20 changes: 20 additions & 0 deletions test/syntax/json/dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"type": "field",
"name": "fillPattern",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -24,6 +25,7 @@
{
"type": "field",
"name": "strokePattern",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -40,6 +42,7 @@
{
"type": "field",
"name": "position",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -53,6 +56,7 @@
{
"type": "field",
"name": "seq",
"required": false,
"idlType": {
"sequence": true,
"generic": "sequence",
Expand All @@ -73,6 +77,20 @@
"type": "sequence",
"value": []
}
},
{
"type": "field",
"name": "reqSeq",
"required": true,
"idlType": {
"sequence": false,
"generic": null,
"nullable": false,
"array": false,
"union": false,
"idlType": "long"
},
"extAttrs": []
}
],
"inheritance": null,
Expand All @@ -86,6 +104,7 @@
{
"type": "field",
"name": "h",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand All @@ -99,6 +118,7 @@
{
"type": "field",
"name": "d",
"required": false,
"idlType": {
"sequence": false,
"generic": null,
Expand Down