Skip to content

Commit 5b70350

Browse files
authored
feat: expose non-top-level production classes (#732)
* feat: expose non-top-level production classes * helper functions
1 parent b261060 commit 5b70350

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

docs/custom-productions.md

+32
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,35 @@ interface Tokeniser {
7878
unconsume(position: number);
7979
}
8080
```
81+
82+
## Using existing productions
83+
84+
This library exposes member productions in `webidl2/productions`. (Note that this only works with ES module import)
85+
86+
* `Argument`
87+
* `Attribute`
88+
* `Base`
89+
* `Constant`
90+
* `Constructor`
91+
* `Container`
92+
* `Default`
93+
* `ExtendedAttributes` / `SimpleExtendedAttribute`
94+
* `Field`
95+
* `IterableLike`
96+
* `Operation`
97+
* `Type`
98+
99+
You can call `Argument.parse(tokeniser)` inside your custom production to reuse it.
100+
101+
It also exposes some helper functions:
102+
103+
* `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.
104+
105+
```js
106+
const ret = autoParenter(this);
107+
ret.default = Default.parse(tokeniser);
108+
default.parent // this
109+
```
110+
111+
* `argument_list`: Receives a tokeniser object and parses arguments separated by commas. Can be used to implement function-like syntax.
112+
* `unescape`: Trims preceding underscore `_` if the string argument has one.

lib/productions/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export { Argument } from "./argument.js";
2+
export { Attribute } from "./attribute.js";
3+
export { Base } from "./base.js";
4+
export { Constant } from "./constant.js";
5+
export { Constructor } from "./constructor.js";
6+
export { Container } from "./container.js";
7+
export { Default } from "./default.js";
8+
export {
9+
ExtendedAttributes,
10+
SimpleExtendedAttribute,
11+
} from "./extended-attributes.js";
12+
export { Field } from "./field.js";
13+
export { IterableLike } from "./iterable.js";
14+
export { Operation } from "./operation.js";
15+
export { Type } from "./type.js";
16+
17+
export { autoParenter, argument_list, unescape } from "./helpers.js";

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@
3939
"repository": "git://github.com/w3c/webidl2.js",
4040
"main": "dist/webidl2.js",
4141
"exports": {
42-
"import": "./index.js",
43-
"require": "./dist/webidl2.js"
42+
".": {
43+
"import": "./index.js",
44+
"require": "./dist/webidl2.js"
45+
},
46+
"./productions": "./lib/productions/index.js"
4447
},
4548
"type": "module",
4649
"files": [

0 commit comments

Comments
 (0)