Skip to content

Alt: #include for stancjs, simpler implementation#1433

Merged
WardBrian merged 16 commits intomasterfrom
feature/stancjs-include-paths-no-functors
Jun 24, 2024
Merged

Alt: #include for stancjs, simpler implementation#1433
WardBrian merged 16 commits intomasterfrom
feature/stancjs-include-paths-no-functors

Conversation

@WardBrian
Copy link
Copy Markdown
Member

Alternative to #1432, also closes #1430

Rather than using functors, this just makes it so the include_paths variable in the Preprocessor now looks like

type include_provider_t =
  | FileSystemPaths of string list
  | InMemory of string String.Map.t

val include_provider : include_provider_t ref

And this is used in the preprocessor to switch between the two styles.

Submission Checklist

  • Run unit tests
  • Documentation
    • If a user-facing facing change was made, the documentation PR is here:
    • OR, no user-facing changes were made

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
Copy link
Copy Markdown

codecov bot commented Jun 21, 2024

Codecov Report

Attention: Patch coverage is 92.59259% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.88%. Comparing base (b46cc7e) to head (e838a56).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1433      +/-   ##
==========================================
+ Coverage   89.83%   89.88%   +0.05%     
==========================================
  Files          63       63              
  Lines       10484    10497      +13     
==========================================
+ Hits         9418     9435      +17     
+ Misses       1066     1062       -4     
Files Coverage Δ
src/frontend/Errors.ml 92.59% <100.00%> (+4.59%) ⬆️
src/middle/Location.ml 93.33% <ø> (ø)
src/stanc/stanc.ml 80.71% <100.00%> (+0.13%) ⬆️
src/frontend/Preprocessor.ml 97.33% <89.47%> (+0.41%) ⬆️

... and 1 file with indirect coverage changes

Comment on lines +260 to +269
Syntax error in 'include/a.stan', line 1, column 0, included from
'include/b.stan', line 1, column 0, included from
'include/a.stan', line 1, column 0, included from
'string', line 1, column 0, include error:
-------------------------------------------------
1: #include <include/a.stan>
^
-------------------------------------------------

File include/b.stan recursively included itself.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it says that the error occurred in file include/a.stan the snipped is from include/b.stan (or rather string which happens to be identical to include/b.stan; Errors.pp does not have access to the includes map.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors.pp doesn't need it, this is because of the lack of the filename-in-msg argument. I have thought a couple times about making that argument essentially required in stancjs, because it's almost always wrong for a caller to not specify it. I've updated the test to read better.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You misunderstood the issue. (This recursive include was a poor example of the general pattern.) Change the includes in the test to

var recursive_a = `// comment here\n#include <include/b.stan>`
var recursive_b = `#include <include/a.stan>\n // comment here`

and you'll see the caret points to nonsense.

Errors.pp calls Errors.pp_context_with_message which reads from the filesystem. It would be possible for whoever calls Errors.pp to check the filename and supply the correct file, but that's not what currently happens, and in my opinion it we should keep the file-finding logic inside Errors.pp_context_with_message.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for that example.

Let me know what you think of my latest commit. To break a circular dependency I needed to move the ref cell out of Preprocessor, but I think the result is decent.

@WardBrian WardBrian requested a review from nhuurre June 24, 2024 15:08
Copy link
Copy Markdown
Collaborator

@nhuurre nhuurre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a couple of nitpicks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#include support in stancjs

2 participants