diff --git a/docs/custom-productions.md b/docs/custom-productions.md index d731663f..3dbaa75b 100644 --- a/docs/custom-productions.md +++ b/docs/custom-productions.md @@ -78,3 +78,35 @@ interface Tokeniser { unconsume(position: number); } ``` + +## Using existing productions + +This library exposes member productions in `webidl2/productions`. (Note that this only works with ES module import) + +* `Argument` +* `Attribute` +* `Base` +* `Constant` +* `Constructor` +* `Container` +* `Default` +* `ExtendedAttributes` / `SimpleExtendedAttribute` +* `Field` +* `IterableLike` +* `Operation` +* `Type` + +You can call `Argument.parse(tokeniser)` inside your custom production to reuse it. + +It also exposes some helper functions: + +* `autoParenter`: This wraps your object in a proxy that assigns any object's `parent` field to `this`. Useful when tracking the parent of member productions. + + ```js + const ret = autoParenter(this); + ret.default = Default.parse(tokeniser); + default.parent // this + ``` + +* `argument_list`: Receives a tokeniser object and parses arguments separated by commas. Can be used to implement function-like syntax. +* `unescape`: Trims preceding underscore `_` if the string argument has one. diff --git a/lib/productions/index.js b/lib/productions/index.js new file mode 100644 index 00000000..4ea3dc22 --- /dev/null +++ b/lib/productions/index.js @@ -0,0 +1,17 @@ +export { Argument } from "./argument.js"; +export { Attribute } from "./attribute.js"; +export { Base } from "./base.js"; +export { Constant } from "./constant.js"; +export { Constructor } from "./constructor.js"; +export { Container } from "./container.js"; +export { Default } from "./default.js"; +export { + ExtendedAttributes, + SimpleExtendedAttribute, +} from "./extended-attributes.js"; +export { Field } from "./field.js"; +export { IterableLike } from "./iterable.js"; +export { Operation } from "./operation.js"; +export { Type } from "./type.js"; + +export { autoParenter, argument_list, unescape } from "./helpers.js"; diff --git a/package.json b/package.json index 0f6ab805..f0959d49 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,11 @@ "repository": "git://github.com/w3c/webidl2.js", "main": "dist/webidl2.js", "exports": { - "import": "./index.js", - "require": "./dist/webidl2.js" + ".": { + "import": "./index.js", + "require": "./dist/webidl2.js" + }, + "./productions": "./lib/productions/index.js" }, "type": "module", "files": [