Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upscalameta code generator JS module #1867
Comments
This comment has been minimized.
This comment has been minimized.
Thank you for reporting! Can you elaborate on what APIs you are currently using and give an example of what is missing? The JavaScript facade is defined here We could add more methods to the facade and publish a new npm update. cc/ @gabro |
This comment has been minimized.
This comment has been minimized.
In my current flow I use const scalametaParsers = require('scalameta-parsers');
const a = '1 + 2';
const b = scalametaParsers.parseStat.call({}, a);
/* =>
{
type: "Term.ApplyInfix", pos: {start: 0, end: 7},
lhs: {
type: "Lit.Int", pos: {start: 1, end: 2},
value: 1, syntax: "1"
},
op: {
type: "Term.Name", pos: {start: 3, end: 4},
value: "+"
},
targs: [],
args: [
{
type: "Lit.Int", pos: {start: 5, end: 6},
value: 2, syntax: "2"
}
]
}
*/ I wish there is some other NPM package that I can use to perform the inverse transformation from AST to text: const codegen = require('scalameta-codegen');
const c = codegen(b);
// => 1 + 2 |
This comment has been minimized.
This comment has been minimized.
@drom do you make transformations on the AST before calling |
This comment has been minimized.
This comment has been minimized.
Typically I programmatically construct new AST and sometimes make transformations of AST from JavaScript. |
This comment has been minimized.
This comment has been minimized.
Thanks for the explanation. I estimate it requires some effort work to map from the JavaScript object into def tree(obj: js.Dynamic): Tree = obj.`type` match {
case "Term.ApplyInfix" => Term.ApplyInfix(tree(obj.lhs), tree(obj.op), tree(obj.targs), tree(obj.args))
// ...
} Some work is needed to make sure everything compiles. Is this something you would be interested in contributing? I'm afraid it's probably not a project that will be prioritized in the near future. |
This comment has been minimized.
This comment has been minimized.
Another approach could be to expose public methods to contruct @JSExport()
def applyInfix(lhs: Term, op: Term.Name, targs: List[Type.Arg], args: List[Term]): Term.ApplyInfix = ... This might be easier to compile than the large pattern match. |
This comment has been minimized.
This comment has been minimized.
Thank you @olafurpg for clarification. I have started writing AST serializer in JavaScript https://github.com/sifive/scala-codegen . That seems pretty straightforward if I could figure out full list of AST node types. |
I am constructing my
scalameta
AST object in JS, then I want to call some JS package to serialize Sacala string. Can someone help me with NPM module that would do that?Here is an example of such a project for JS : https://github.com/estools/escodegen