Skip to content

Commit

Permalink
Merge pull request #1492 from stan-dev/fix/1491-stancjs-model-name
Browse files Browse the repository at this point in the history
Restore previous behavior of model name arguments
  • Loading branch information
WardBrian authored Jan 21, 2025
2 parents d13e71e + b8fd154 commit 1b601d5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/driver/Entry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ let fmt_sexp s =
Sexp.pp_hum ppf s;
Format.flush_str_formatter ()

let mangle =
String.concat_map ~f:(fun c ->
Char.(
if is_alphanum c || c = '_' then to_string c
else match c with '-' -> "_" | _ -> "x" ^ Int.to_string (to_int c)))
let set_model_name model_name =
let mangle =
String.concat_map ~f:(fun c ->
Char.(
if is_alphanum c || c = '_' then to_string c
else match c with '-' -> "_" | _ -> "x" ^ Int.to_string (to_int c)))
in
let model_name_munged =
Flags.remove_dotstan List.(hd_exn (rev (String.split model_name ~on:'/')))
in
if String.equal model_name model_name_munged then
(* model name was not file-like, so we leave as is (e.g. from --name argument) *)
Typechecker.model_name := mangle model_name
else
(* model name was a file-like thing, so we add _model to match existing behavior *)
Typechecker.model_name := mangle (model_name_munged ^ "_model")

let reset_mutable_states model_name (flags : Flags.t) =
Common.Gensym.reset_danger_use_cautiously ();
Include_files.include_provider := flags.include_source;
if String.equal !Typechecker.model_name "" then
Typechecker.model_name :=
mangle
(Flags.remove_dotstan
List.(hd_exn (rev (String.split model_name ~on:'/'))))
^ "_model"
else Typechecker.model_name := mangle !Typechecker.model_name;
set_model_name model_name;
Typechecker.check_that_all_functions_have_definition :=
not flags.allow_undefined;
Transform_Mir.use_opencl := flags.use_opencl;
Expand Down
5 changes: 3 additions & 2 deletions src/stanc/stanc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ let main () =
| Compile settings -> settings in
Debugging.lexer_logging := debug_lex;
Debugging.grammar_logging := debug_parse;
Typechecker.model_name := Option.value ~default:"" name;
Driver.Flags.set_backend_args_list
(* remove executable itself from list before passing *)
(Sys.get_argv () |> Array.to_list |> List.tl_exn);
Expand All @@ -65,7 +64,9 @@ let main () =
, Option.first_some flags.filename_in_msg (Some "stdin") )
else (model_file, `File model_file, flags.filename_in_msg) in
match
Driver.Entry.stan2cpp model_file_name model_source flags
Driver.Entry.stan2cpp
(Option.value ~default:model_file_name name)
model_source flags
(output_callback output_file printed_filename)
with
| Ok cpp_str ->
Expand Down
15 changes: 15 additions & 0 deletions test/integration/cli-args/model-name/stanc.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Test model name mangeling for non-alphanumeric characters
$ echo 'parameters {real y;}' >> añd.stan
Filename
$ stanc añd.stan
$ grep "namespace a" añd.hpp
namespace ax195x177d_model_namespace {
Name argument
$ stanc --name=añd añd.stan
$ grep "namespace a" añd.hpp
namespace ax195x177d_namespace {
Name argument with dash
$ stanc --name="a-d" añd.stan
$ grep "namespace a" añd.hpp
namespace a_d_namespace {
$ rm añd.hpp añd.stan
29 changes: 29 additions & 0 deletions test/stancjs/model_name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var stanc = require('../../src/stancjs/stancjs.bc.js');
var utils = require("./utils/utils.js");


let basic_model = `
parameters {
real y;
}
model {
y ~ std_normal();
}
`

let basic_name1 = stanc.stanc("basic1", basic_model, []);
utils.print_error(basic_name1);
console.assert(basic_name1.result.includes("namespace basic1_namespace"), "Error: namespace not in C++ code");

// test that the state doesn't persist

let basic_name2 = stanc.stanc("basic2", basic_model, []);
utils.print_error(basic_name2);
console.assert(basic_name2.result.includes("namespace basic2_namespace"), "Error: namespace not in C++ code");


// test that _model is added if the name is file-like

let basic_name3 = stanc.stanc("basic.stan", basic_model, []);
utils.print_error(basic_name3);
console.assert(basic_name3.result.includes("namespace basic_model_namespace"), "Error: namespace not in C++ code");
2 changes: 2 additions & 0 deletions test/stancjs/stancjs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ $ node info.js
}
$ node math_sigs.js

$ node model_name.js

$ node optimization.js
Semantic error in 'string', line 3, column 8 to column 11:
-------------------------------------------------
Expand Down

0 comments on commit 1b601d5

Please sign in to comment.