Skip to content

Conversation

zth
Copy link
Member

@zth zth commented Jun 21, 2024

Experimenting with integrating https://github.com/zth/rescript-embed-lang into the compiler.

Instructions

Pass whatever extension points you want to treat as embeds to bsc via -embed when producing the .ast file via -bs-ast:

./bsc tst.res -bs-ast -embed sql.one -embed sql.many

Some examples. Imagine this ReScript file:

// tst.res
let query1 = %sql.one(`
  SELECT * FROM tst.res
  WHERE id = 1
`)

let q2 = %sql.many(`
  SELECT * FROM tst.res
  WHERE id > 1
`)

let q3 = %sql.one(`
  SELECT * FROM tst.res
  WHERE id = 2
`)

module Query = %sql.many(`
  SELECT * FROM tst.res
  WHERE id > 2
`)

Running ./bsc tst.res -bs-ast -embed sql.one -embed sql.many will do 2 things:

First, it'll produce a JSON file tst.embeds.json right next to the tst.ast file, with all embeds + info needed for them from the file:

[
  {
    "tag": "sql.one",
    "filename": "Tst__sql_one_1.res",
    "contents": "\n  SELECT * FROM tst.res\n  WHERE id = 1\n",
    "loc": {"start": {"line": 1, "col": 22}, "end": {"line": 4, "col": 64}}
  },
  {
    "tag": "sql.many",
    "filename": "Tst__sql_many_1.res",
    "contents": "\n  SELECT * FROM tst.res\n  WHERE id > 1\n",
    "loc": {"start": {"line": 6, "col": 86}, "end": {"line": 9, "col": 128}}
  },
  {
    "tag": "sql.one",
    "filename": "Tst__sql_one_2.res",
    "contents": "\n  SELECT * FROM tst.res\n  WHERE id = 2\n",
    "loc": {"start": {"line": 11, "col": 149}, "end": {"line": 14, "col": 191}}
  },
  {
    "tag": "sql.many",
    "filename": "Tst__sql_many_2.res",
    "contents": "\n  SELECT * FROM tst.res\n  WHERE id > 2\n",
    "loc": {"start": {"line": 16, "col": 219}, "end": {"line": 19, "col": 261}}
  }
]

Second, it'll rewrite the source to point to the generated file it expects:

// tst.res, the source as processed by the embed PPX
let query1 = Tst__sql_one_1.default

let q2 = Tst__sql_many_1.default

let q3 = Tst__sql_one_2.default

module Query = Tst__sql_many_2
  1. For extension points in the let binding position , the PPX transforms the let binding to access .default inside of the generated module.
  2. For extension points as modules, it transforms to access the full module.

TODO

  • Add tests

Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Old issues that went stale label Sep 15, 2025
@zth
Copy link
Member Author

zth commented Sep 15, 2025

This will happen, but needs a full reboot. Closing this for now, but it can serve as inspiration for the future.

@zth zth closed this Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Old issues that went stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant