-
Notifications
You must be signed in to change notification settings - Fork 24
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
emitting non-strings #84
Comments
It occurs to me that I could just write something which walks over the grammar's |
The easiest way to do this with a custom emitter would be to use class ObjectEmitter extends Emitter {
document: HTMLDocument;
currentNode: HTMLElement;
...
protected emitProduction(node: Production) {
const linkId = this.resolver.getProductionLinkId(node.name);
this.emitLinkAnchor(linkId);
const node = this.document.createElement("emu-production");
node.setAttribute("name", node.name.text);
// push the new node as the currentNode
const container = this.currentNode;
this.currentNode = node;
// NOTE: parameterList is written to attributes on `this.currentNode`
this.emitNode(node.parameterList);
if (node.colonToken) {
switch (node.colonToken.kind) {
case SyntaxKind.ColonColonToken:
node.setAttribute("type", "lexical");
break;
case SyntaxKind.ColonColonColonToken:
node.setAttribute("type", "regexp");
break;
}
}
if (node.body) {
if (node.body.kind === SyntaxKind.OneOfList) {
node.setAttribute("oneof", "oneof");
}
else if (node.body.kind === SyntaxKind.RightHandSide) {
node.setAttribute("collapsed", "collapsed");
}
}
this.emitNode(node.body);
container.appendChild(this.currentNode);
// restore the previous node
this.currentNode = container;
}
...
private emitLinkAnchor(linkId: string | undefined) {
if (linkId && this.options.emitLinks) {
const link = this.document.createElement("a");
link.setAttribute("name", linkId);
this.currentNode.appendChild(link);
}
}
} and elsewhere: const document = ...; // JSDom document
const rootElement = ...; // JSDom element or maybe a document fragment
const grammar = ...;
await grammar.check();
for (const rootFile of grammar.rootFiles) {
const emitter = new CustomEmitter(document, rootElement);
emitter.emitString(rootFile, grammer.resolver, grammar.diagnostics);
}
Mostly because const grammar = new Grammar(files);
await grammar.emit(); Even so, its still designed to be fairly flexible. You can subclass |
For ecmarkup, I need to associate the generated HTML productions with their original
Production
objects. The obvious way to do that is to make an emitter which creates JSDom nodes directly and sticks them in a WeakMap pointing back to the original object. But the Emitter API is string-based. Would it be possible to allow more general emitters which support emitting other types?My current workaround is to assume that the
production
andemu-rhs
elements are in the same order as in the original and index each list, which seems to work. So there's no urgency in supporting this.Sidebar: as a question of API design, why is the emitter a property of the grammar? I would expect it to be passed in to the
emit
method. (That would be helpful here because then theemit
method could have a type parameter for the type produced by the emitter, which doesn't work when the emitter is state instead of being an argument.)The text was updated successfully, but these errors were encountered: