Skip to content
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

scalameta code generator JS module #1867

Open
drom opened this issue May 15, 2019 · 7 comments
Open

scalameta code generator JS module #1867

drom opened this issue May 15, 2019 · 7 comments

Comments

@drom
Copy link

@drom drom commented May 15, 2019

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

@olafurpg

This comment has been minimized.

Copy link
Member

@olafurpg olafurpg commented Nov 16, 2019

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

https://github.com/scalameta/scalameta/blob/7c5c020cc0ff9b97d9029b315fb8659590236a2c/scalameta/parsers/js/src/main/scala/scala/meta/parsers/JSFacade.scala

We could add more methods to the facade and publish a new npm update.

cc/ @gabro

@drom

This comment has been minimized.

Copy link
Author

@drom drom commented Nov 16, 2019

In my current flow I use scalameta-parsers NPM package and can call parser to construct AST:

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
@olafurpg

This comment has been minimized.

Copy link
Member

@olafurpg olafurpg commented Nov 16, 2019

@drom do you make transformations on the AST before calling codegen?

@drom

This comment has been minimized.

Copy link
Author

@drom drom commented Nov 16, 2019

Typically I programmatically construct new AST and sometimes make transformations of AST from JavaScript.

@olafurpg

This comment has been minimized.

Copy link
Member

@olafurpg olafurpg commented Nov 16, 2019

Thanks for the explanation. I estimate it requires some effort work to map from the JavaScript object into scala.meta.Tree, it would be a large pattern match that looks something like this

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.

@olafurpg

This comment has been minimized.

Copy link
Member

@olafurpg olafurpg commented Nov 16, 2019

Another approach could be to expose public methods to contruct scala.meta.Tree instances, for example

@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.

@drom

This comment has been minimized.

Copy link
Author

@drom drom commented Nov 16, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.