Skip to content
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
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"browser": true,
"commonjs": true,
"amd": true,
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended",
"rules": {
"no-constant-condition": "off",
"prefer-const": "error",
"semi": "error"
}
}
18 changes: 9 additions & 9 deletions lib/webidl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"identifier": /_?[A-Za-z][0-9A-Z_a-z-]*/y,
"string": /"[^"]*"/y,
"whitespace": /[\t\n\r ]+/y,
"comment": /((\/(\/.*|\*([^*]|\*[^\/])*\*\/)[\t\n\r ]*)+)/y,
"comment": /((\/(\/.*|\*([^*]|\*[^/])*\*\/)[\t\n\r ]*)+)/y,
"other": /[^\t\n\r 0-9A-Za-z]/y
};

Expand Down Expand Up @@ -182,7 +182,6 @@
const INT = "integer";
const ID = "identifier";
const STR = "string";
const OTHER = "other";

const EMPTY_OPERATION = Object.freeze({
type: "operation",
Expand Down Expand Up @@ -218,7 +217,7 @@

let message;
if (current) {
message = `Got an error during or right after parsing \`${current.partial ? "partial " : ""}${current.type} ${current.name}\`: ${str}`
message = `Got an error during or right after parsing \`${current.partial ? "partial " : ""}${current.type} ${current.name}\`: ${str}`;
}
else {
// throwing before any valid definition
Expand All @@ -244,6 +243,7 @@

function consume(...candidates) {
// TODO: use const when Servo updates its JS engine
// eslint-disable-next-line prefer-const
for (let type of candidates) {
if (!probe(type)) continue;
const token = tokens[consume_position];
Expand Down Expand Up @@ -306,7 +306,7 @@
prefix,
baseName: base.value,
trivia: { base: base.trivia }
}
};
}
if (prefix) error("Failed to parse float type");
}
Expand All @@ -320,7 +320,7 @@
idlType: base.value,
baseName: base.value,
trivia: { base: base.trivia }
}
};
}
}

Expand Down Expand Up @@ -379,7 +379,7 @@
case "FrozenArray":
ret.idlType = [type_with_extended_attributes(typeName)];
break;
case "record":
case "record": {
if (probe("[")) error("Record key cannot have extended attribute");
ret.idlType = [];
const keyType = consume(...stringTypes);
Expand All @@ -396,6 +396,7 @@
const valueType = type_with_extended_attributes(typeName) || error("Error parsing generic type record");
ret.idlType.push(valueType);
break;
}
}
if (!ret.idlType) error(`Error parsing generic type ${name.type}`);
const close = consume(">") || error(`Missing closing bracket after ${name.type}`);
Expand All @@ -406,7 +407,6 @@
function single_type(typeName) {
const ret = Object.assign({ type: typeName || null }, EMPTY_IDLTYPE, { trivia: {} });
const base = generic_type(typeName) || primitive_type();
let name;
if (base) {
Object.assign(ret, base);
} else {
Expand Down Expand Up @@ -691,7 +691,7 @@
trivia: {}
};
if (!noInherit) {
const inherit = consume("inherit")
const inherit = consume("inherit");
if (inherit) {
ret.inherit = { trivia: inherit.trivia };
}
Expand Down Expand Up @@ -895,7 +895,7 @@
const trivia = {
base: null,
mixin: mixin.trivia
}
};
const name = consume(ID) || error("No name for interface mixin");
const mems = [];
const ret = current = {
Expand Down
48 changes: 24 additions & 24 deletions lib/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
if (it.separator) ret += it.separator.trivia + it.separator.value;

return ret;
};
}
function const_value(it) {
const tp = it.type;
if (tp === "boolean") return it.value ? "true" : "false";
Expand All @@ -25,7 +25,7 @@
else if (tp === "NaN") return "NaN";
else if (tp === "number") return it.value;
else return `"${it.value}"`;
};
}
function default_(def) {
const assign = `${def.trivia.assign}=`;
if (def.type === "sequence") {
Expand All @@ -42,7 +42,7 @@
if (arg.default) ret += default_(arg.default);
if (arg.separator) ret += `${arg.separator.trivia}${arg.separator.value}`;
return ret;
};
}
function identifier(id) {
let ret = id.trivia + id.value;
if (id.separator) ret += id.separator.trivia + id.separator.value;
Expand All @@ -57,11 +57,11 @@
if (it.signature) ret += `${it.signature.trivia.open}(${it.signature.arguments.map(argument).join("")}${it.signature.trivia.close})`;
if (it.separator) ret += `${it.separator.trivia}${it.separator.value}`;
return ret;
};
}
function extended_attributes(eats) {
if (!eats) return "";
return `${eats.trivia.open}[${eats.items.map(make_ext_at).join("")}${eats.trivia.close}]`;
};
}

function operation(it) {
let ret = extended_attributes(it.extAttrs);
Expand All @@ -76,7 +76,7 @@
}
ret += it.trivia.termination + ";";
return ret;
};
}

function attribute(it) {
let ret = extended_attributes(it.extAttrs);
Expand All @@ -86,7 +86,7 @@
if (it.readonly) ret += `${it.readonly.trivia}readonly`;
ret += `${it.trivia.base}attribute${type(it.idlType)}${it.trivia.name}${it.escapedName};`;
return ret;
};
}

function inheritance(inh) {
return `${inh.trivia.colon}:${inh.trivia.name}${inh.name}`;
Expand All @@ -99,7 +99,7 @@
if (it.inheritance) ret += inheritance(it.inheritance);
ret += `${it.trivia.open}{${iterate(it.members)}${it.trivia.close}}${it.trivia.termination};`;
return ret;
};
}

function interface_mixin(it) {
let ret = extended_attributes(it.extAttrs);
Expand All @@ -124,49 +124,49 @@
if (it.inheritance) ret += inheritance(it.inheritance);
ret += `{${iterate(it.members)}};`;
return ret;
};
}
function field(it) {
let ret = extended_attributes(it.extAttrs);
if (it.required) ret += "required";
ret += `${type(it.idlType)} ${it.escapedName}`;
if (it.default) ret += default_(it.default);
ret += ";";
return ret;
};
}
function const_(it) {
let ret = extended_attributes(it.extAttrs);
const ret = extended_attributes(it.extAttrs);
return `${ret}${it.trivia.base}const${type(it.idlType)}${it.trivia.name}${it.name}${it.trivia.assign}=${it.trivia.value}${const_value(it.value)}${it.trivia.termination};`;
};
}
function typedef(it) {
let ret = extended_attributes(it.extAttrs);
const ret = extended_attributes(it.extAttrs);
return `${ret}typedef${type(it.idlType)} ${it.name};`;
};
}
function implements_(it) {
const ret = extended_attributes(it.extAttrs);
return `${ret}${it.target} implements ${it.implements};`;
};
}
function includes(it) {
const ret = extended_attributes(it.extAttrs);
return `${ret}${it.target} includes ${it.includes};`;
};
}
function callback(it) {
const ret = extended_attributes(it.extAttrs);
const args = it.arguments.map(argument).join("");
return `${ret}callback ${it.name} =${type(it.idlType)}(${args});`;
};
}
function enum_(it) {
const ext = extended_attributes(it.extAttrs);
const values = it.values.map(v => `"${v.value}",`).join("");
return `${ext}enum ${it.name} {${values}};`;
};
}
function iterable_like(it) {
const readonly = it.readonly ? `${it.readonly.trivia}readonly` : "";
const bracket = `${it.trivia.open}<${it.idlType.map(type).join("")}${it.trivia.close}>`;
return `${readonly}${it.trivia.type}${it.type}${bracket}${it.trivia.termination};`;
};
}
function callbackInterface(it) {
return `callback${interface_(it)}`;
};
}

const table = {
interface: interface_,
Expand All @@ -191,18 +191,18 @@
function dispatch(it) {
const dispatcher = table[it.type];
if (!dispatcher) {
throw new Error(`Type "${it.type}" is unsupported`)
throw new Error(`Type "${it.type}" is unsupported`);
}
return table[it.type](it);
};
}
function iterate(things) {
if (!things) return;
let ret = "";
for (const thing of things) ret += dispatch(thing);
return ret;
};
}
return iterate(ast);
};
}


const obj = {
Expand Down
Loading