-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow railroad diagrams to be specified in JSON and generated with a single invocation #20
Comments
Because JS calls work great for just generating a diagram, and are simpler to write. l assume you want to pass diagram data around as JSON and then generate a diagram from it afterwards? That seems like a pretty useful thing to do, so I'm okay with adding this functionality. |
Is just that: the separation of data and program phylosophy. Thanks in advance. |
Keeping this open to track the request. |
I’ve made this little script and it might be helpful (i don’t test all the possibilities). function createDiagram(node) {
let stack = [];
if (Array.isArray(node)) {
for (let value of node) {
if (typeof value.value === 'object') {
if (typeof window[value.type] === 'function') {
stack.push(window[value.type].apply(null, createDiagram(value.value)));
}
} else {
stack.push.apply(stack, createDiagram.call(this, value));
}
}
} else if (typeof window[node.type] === 'function') {
stack.push(window[node.type](node.value));
}
return stack;
} Usage let obj = [
{ type: 'Terminal', value: 'yield' },
{ type: 'Terminal', value: '[' },
{ type: 'NonTerminal', value: 'yield_arg' },
{ type: 'Terminal', value: ']' },
{ type: 'Stack', value: [
{ type: 'Terminal', value: 'stacked!' },
{ type: 'Terminal', value: 'yaaay' }
]}
];
let diagram = Diagram.apply(null, createDiagram(obj)); |
What's the status? |
While implementing a method parsing the diagram from JSON is straightforward, deciding on the format may need more discussion. For example,. mine got inspired by the JavaScript AST from the ESTree specification. |
After rewriting all examples from While JSON or YAML are good for machine generation ans consumption, I wonder if it makes sense to have some other DSL for hand-made diagrams, apart from the JavaScript functions. For example:
It would speed up writing diagrams manually, but would anyone learn a new "RRD" language? |
In fact I already have exactly such a DSL for my Bikeshed tool, which I just haven't ported back to the mainline repo yet: code is https://github.com/tabatkins/bikeshed/blob/master/bikeshed/railroadparser.py, docs are https://tabatkins.github.io/bikeshed/#railroad |
Seeing as there wasn't much activity here and that what prantlf did was only for js, I took it on myself to make a full-fledged python CLI (https://github.com/Epithumia/pyrailroad) to parse DSL/JSON/YAML source diagrams based on your code, if it's any use for others looking at the issue here. In the coming days/weeks (my holidays are over very soon, and due to extensive reformatting/sonarqube QC, I can't just diff files and be done, so it'll take a little time to upstream everything), I'll PR some bugfixes/additions I encountered/made in both railroad.py (like the duplicated text elements when using href in Terminal/NonTerminal/Comment) and the bikeshed parser (adding unsupported DiagramItems and allowing urls in the Terminal/NonTerminal/Comment preludes, notably). |
Why "ZeroOrMore", "Choice", etc. are nested JS function calls?
Why are you not implemented it as JSON (JS objects)?
The text was updated successfully, but these errors were encountered: