More tests and some fixes

This commit is contained in:
Louis Gesbert 2023-11-30 23:49:19 +01:00
parent 3649f92975
commit 8df49dcea2
6 changed files with 68 additions and 20 deletions

View File

@ -598,7 +598,7 @@ let gen_build_statements
| Some m ->
let target ext = (!Var.builddir / src /../ m) ^ "." ^ ext in
Nj.build "ocaml-module" ~inputs:[ml_file]
~implicit_in:(List.map modd modules)
~implicit_in:(List.map (modfile ".cmi") modules)
~outputs:[target "cmxs"]
~implicit_out:(List.map target implicit_out_exts)
~vars:

View File

@ -819,6 +819,15 @@ let line_dir_arg_re =
eol
])
let line_dir_arg_upcase_re =
Re.(compile @@ seq [
bos; char '>'; rep space; rep1 alpha;
rep (alt [space; lower]); space;
group (seq [rep1 upper; rep (diff any space)]);
rep any;
eol
])
let lex_line (lexbuf : lexbuf) : (string * L.line_token) option =
match%sedlex lexbuf with
| eof -> None
@ -844,13 +853,13 @@ let lex_line (lexbuf : lexbuf) : (string * L.line_token) option =
| '>', Star hspace, MR_MODULE_DEF, Plus hspace, uppercase, Star (Compl '\n'), ('\n' | eof) ->
let str = Utf8.lexeme lexbuf in
(try
let mdl = Re.Group.get (Re.exec line_dir_arg_re str) 1 in
let mdl = Re.Group.get (Re.exec line_dir_arg_upcase_re str) 1 in
Some (str, LINE_MODULE_DEF mdl)
with Not_found -> Some (str, LINE_ANY))
| '>', Star hspace, MR_MODULE_USE, Plus hspace, uppercase, Star (Compl '\n'), ('\n' | eof) ->
let str = Utf8.lexeme lexbuf in
(try
let mdl = Re.Group.get (Re.exec line_dir_arg_re str) 1 in
let mdl = Re.Group.get (Re.exec line_dir_arg_upcase_re str) 1 in
Some (str, LINE_MODULE_USE mdl)
with Not_found -> Some (str, LINE_ANY))
| Star (Compl '\n'), ('\n' | eof) -> Some (Utf8.lexeme lexbuf, LINE_ANY)

View File

@ -393,29 +393,32 @@ let with_sedlex_source source_file f =
Sedlexing.set_filename lexbuf file;
f lexbuf
let check_modname program source_file =
match program.Ast.program_module_name, source_file with
| Some (mname, pos), (Cli.FileName file | Cli.Contents (_, file) | Cli.Stdin file)
when not File.(equal mname Filename.(remove_extension (basename file))) ->
Message.raise_spanned_error pos
"Module declared as @{<blue>%s@}, which does not match the file name %a"
mname
File.format file
| _ -> ()
let load_interface source_file =
let program = with_sedlex_source source_file parse_source in
check_modname program source_file;
let modname =
match program.Ast.program_module_name, source_file with
| Some (mname, pos), Cli.FileName file ->
if File.(equal mname Filename.(remove_extension (basename file)))
then mname, pos
else
Message.raise_spanned_error pos
"Module declared as @{<blue>%s@}, which does not match the file name %a"
mname
File.format file
| Some mname, _ -> mname
| None, _ ->
match program.Ast.program_module_name with
| Some mname -> mname
| None ->
Message.raise_error
"%a doesn't define a module name. It should contain a '@{<cyan>> \
Module %s@}' directive."
File.format
(Cli.input_src_file source_file)
(match source_file with
| FileName s ->
String.capitalize_ascii Filename.(basename (remove_extension s))
| _ -> "Module_name")
| FileName s ->
String.capitalize_ascii Filename.(basename (remove_extension s))
| _ -> "Module_name")
in
let used_modules, intf = get_interface program in
{ Ast.intf_modname = modname;
@ -424,6 +427,7 @@ let load_interface source_file =
let parse_top_level_file (source_file : Cli.input_src) : Ast.program =
let program = with_sedlex_source source_file parse_source in
check_modname program source_file;
{
program with
Ast.program_items = law_struct_list_to_tree program.Ast.program_items;

View File

@ -0,0 +1,21 @@
> Include: mod_badname.catala_en
```catala-test-inline
$ catala typecheck
[ERROR]
A file that declares a module cannot be used through the raw '> Include' directive. You should use it as a module with '> Use This_is_not_the_file_name' instead.
File include
┌─⯈ tests/test_modules/bad/mod_bad_include.catala_en:1.3-1.33:
└─┐
1 │ > Include: mod_badname.catala_en
│ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Module declaration
┌─⯈ tests/test_modules/bad/mod_badname.catala_en:1.10-1.35:
└─┐
1 │ > Module This_is_not_the_file_name
│ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
#return code 123#
```

View File

@ -0,0 +1,14 @@
> Module This_is_not_the_file_name
```catala-test-inline
$ catala typecheck
[ERROR]
Module declared as This_is_not_the_file_name, which does not match the file name "tests/test_modules/bad/mod_badname.catala_en"
┌─⯈ tests/test_modules/bad/mod_badname.catala_en:1.10-1.35:
└─┐
1 │ > Module This_is_not_the_file_name
│ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
#return code 123#
```

View File

@ -1,10 +1,10 @@
> Using Mod_middle
> Using Mod_middle as M
```catala
declaration scope T:
t1 scope Mod_middle.S
t1 scope M.S
# input i content Enum1
output o1 content Mod_middle.Mod_def.S
output o1 content M.Mod_def.S
output o2 content money
output o3 content money