From cb2ac2fecfa0dec231db15be0bef1e54be66abbe Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Thu, 13 Apr 2023 15:21:12 +0200 Subject: [PATCH 1/2] feat: expose non-top-level production classes --- docs/custom-productions.md | 19 +++++++++++++++++++ lib/productions/index.js | 17 +++++++++++++++++ package.json | 7 +++++-- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 lib/productions/index.js diff --git a/docs/custom-productions.md b/docs/custom-productions.md index d731663f..1c4b4caf 100644 --- a/docs/custom-productions.md +++ b/docs/custom-productions.md @@ -78,3 +78,22 @@ 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. diff --git a/lib/productions/index.js b/lib/productions/index.js new file mode 100644 index 00000000..020ace3b --- /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 } from "./helpers.js"; diff --git a/package.json b/package.json index a0d4de1b..3000a235 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": [ From cd3ce7e53ae99ece1014f77e85aa3ca1a8321b67 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Thu, 13 Apr 2023 15:59:13 +0200 Subject: [PATCH 2/2] helper functions --- docs/custom-productions.md | 13 +++++++++++++ lib/productions/index.js | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/custom-productions.md b/docs/custom-productions.md index 1c4b4caf..3dbaa75b 100644 --- a/docs/custom-productions.md +++ b/docs/custom-productions.md @@ -97,3 +97,16 @@ This library exposes member productions in `webidl2/productions`. (Note that thi * `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 index 020ace3b..4ea3dc22 100644 --- a/lib/productions/index.js +++ b/lib/productions/index.js @@ -14,4 +14,4 @@ export { IterableLike } from "./iterable.js"; export { Operation } from "./operation.js"; export { Type } from "./type.js"; -export { autoParenter } from "./helpers.js"; +export { autoParenter, argument_list, unescape } from "./helpers.js";