stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Allow `#include` statements to function in stanc.js #1432

Closed WardBrian closed 1 week ago

WardBrian commented 1 week ago

Closes #1430

This PR can be viewed as several parts:

  1. Some boilerplate-y code in stancjs.ml to read a { [s: string] : string } JS object into a ocaml string String.Map.t
  2. Extracting the logic in the Preprocessor that looks up new included files into a module type that is passed in from the outside
  3. Doing the same in the Lexer

An earlier version of this didn't use functors and just had Preprocessor store a function pointer that was set by stancjs.ml/stanc.ml, but I had both aesthetic and performance concerns with that approach. These functors are ultimately pretty lightweight

This is currently lightly tested in stanc.js but I intend to add more.

Submission Checklist

Release notes

stanc.js can now accept models which contain #include statements. A fourth argument is available on the javascript stanc function which must be either undefined or a object mapping included file names to Stan source code as strings.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

codecov[bot] commented 1 week ago

Codecov Report

Attention: Patch coverage is 81.81818% with 10 lines in your changes missing coverage. Please review.

Project coverage is 89.75%. Comparing base (b46cc7e) to head (03115e1).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1432 +/- ## ========================================== - Coverage 89.83% 89.75% -0.08% ========================================== Files 63 65 +2 Lines 10484 10495 +11 ========================================== + Hits 9418 9420 +2 - Misses 1066 1075 +9 ``` | [Files](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev) | Coverage Δ | | |---|---|---| | [src/frontend/Filesystem\_includes.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Ffrontend%2FFilesystem_includes.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL0ZpbGVzeXN0ZW1faW5jbHVkZXMubWw=) | `100.00% <100.00%> (ø)` | | | [src/frontend/Parse.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Ffrontend%2FParse.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL1BhcnNlLm1s) | `81.57% <100.00%> (+1.57%)` | :arrow_up: | | [src/frontend/Preprocessor.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Ffrontend%2FPreprocessor.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL1ByZXByb2Nlc3Nvci5tbA==) | `96.22% <100.00%> (-0.70%)` | :arrow_down: | | [src/stanc/stanc.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Fstanc%2Fstanc.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL3N0YW5jL3N0YW5jLm1s) | `80.71% <100.00%> (+0.13%)` | :arrow_up: | | [src/frontend/Pretty\_print\_prog.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Ffrontend%2FPretty_print_prog.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL1ByZXR0eV9wcmludF9wcm9nLm1s) | `82.35% <80.00%> (-0.51%)` | :arrow_down: | | [src/frontend/In\_memory\_includes.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1432?src=pr&el=tree&filepath=src%2Ffrontend%2FIn_memory_includes.ml&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL0luX21lbW9yeV9pbmNsdWRlcy5tbA==) | `0.00% <0.00%> (ø)` | |
WardBrian commented 1 week ago

Superseded by #1433