Skip to content

Commit 4f55a63

Browse files
committed
Refine an independent IRJ test files parser. Compatibility with both primary and corrective test files.
1 parent 1d4ead0 commit 4f55a63

File tree

5 files changed

+101
-51
lines changed

5 files changed

+101
-51
lines changed

examples/ocaml/parser/fip.ml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(*From test_interpreter.ml*)
22

3-
let parse_file (test_name : string) : Types_module.test_file =
3+
open Types_module
4+
5+
let parse_file (test_name : string) : Types_module.irj_file =
46
let input = open_in test_name in
57
let filebuf = Lexing.from_channel input in
68
let filebuf =
@@ -10,14 +12,27 @@ let parse_file (test_name : string) : Types_module.test_file =
1012
}
1113
in
1214
let f =
13-
try Test_parser.test_file Test_lexer.token filebuf with
15+
try Test_parser.irj_file Test_lexer.token filebuf with
1416
| Types_module.StructuredError e ->
1517
close_in input;
1618
raise (Types_module.StructuredError e)
1719
| Test_parser.Error ->
1820
close_in input;
1921
Types_module.raise_spanned_error "Test syntax error"
2022
(Types_module.mk_position (filebuf.lex_start_p, filebuf.lex_curr_p))
21-
in
23+
in
2224
close_in input;
23-
f
25+
f
26+
(*
27+
let () =
28+
let donnees = parse_file "alavoine-2.irj" in
29+
let (entrees, _, _) = donnees.prim in
30+
print_string donnees.nom;print_newline();
31+
let (code, valeur, _) = List.hd entrees in
32+
print_string "première entrée : "; print_string code; print_string " avec la valeur ";
33+
match valeur with
34+
| I entier -> print_int entier; print_string " entière.";print_newline();flush stdout
35+
| F flottant -> print_float flottant; print_string " flottante.";
36+
print_newline();
37+
flush stdout
38+
*)

examples/ocaml/parser/test_lexer.mll

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,29 @@ rule token = parse
3636
| "#FIP"
3737
{ FIP }
3838
| "#ENTREES-PRIMITIF"
39-
{ ENTREESP }
39+
{ ENTREESPRIM }
4040
| "#CONTROLES-PRIMITIF"
41-
{ CONTROLESP }
41+
{ CONTROLESPRIM }
4242
| "#RESULTATS-PRIMITIF"
43-
{ RESULTATSP }
43+
{ RESULTATSPRIM }
4444
| "#ENTREES-CORRECTIF"
45-
{ ENTREESC }
45+
{ ENTREESCORR }
4646
| "#CONTROLES-CORRECTIF"
47-
{ CONTROLESC }
47+
{ CONTROLESCORR }
4848
| "#RESULTATS-CORRECTIF"
49-
{ RESULTATSC }
50-
| "#DATES"
49+
{ RESULTATSCORR }
50+
| "#ENTREES-RAPPELS"
51+
{ ENTREESRAPP }
52+
| "#CONTROLES-RAPPELS"
53+
{ CONTROLESRAPP }
54+
| "#RESULTATS-RAPPELS"
55+
{ RESULTATSRAPP }
56+
(*| "#DATES"
5157
{ DATES }
5258
| "#AVIS_IR"
5359
{ AVISIR }
5460
| "#AVIS_CSG"
55-
{ AVISCSG }
61+
{ AVISCSG }*)
5662
| "##"
5763
{ ENDSHARP }
5864
| '-'? ['0' - '9']+ as i

examples/ocaml/parser/test_parser.mly

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,51 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
%}
2121

2222
%token<string> SYMBOL NAME INTEGER FLOAT
23+
/* Possibly use stronger constraints than just string on rappel's fields
24+
some are characters, some are 0/1, etc. */
2325

2426
%token SLASH
2527
%token NOM FIP
26-
%token ENTREESP CONTROLESP RESULTATSP
27-
%token ENTREESC CONTROLESC RESULTATSC
28-
%token DATES AVISIR AVISCSG
28+
%token ENTREESPRIM CONTROLESPRIM RESULTATSPRIM
29+
%token ENTREESCORR CONTROLESCORR RESULTATSCORR
30+
%token ENTREESRAPP CONTROLESRAPP RESULTATSRAPP
31+
/* %token DATES AVISIR AVISCSG*/
2932
%token ENDSHARP
3033

3134
%token EOF
3235

33-
%type<Types_module.test_file> test_file
36+
%type<Types_module.irj_file> irj_file
3437

35-
%start test_file
38+
%start irj_file
3639

3740
%%
3841

39-
40-
41-
test_file:
42+
irj_file:
4243
| NOM nom = name
4344
fip?
44-
ENTREESP
45-
ep = list(variable_and_value)
46-
CONTROLESP
47-
cp = list(variable_and_value)
48-
RESULTATSP
49-
rp = list(variable_and_value)
50-
corr = correctif?
51-
DATES?
52-
AVISIR?
53-
AVISCSG?
54-
ENDSHARP { { nom; ep; cp; rp; corr } }
45+
prim = primitif
46+
rapp = rappels
47+
ENDSHARP { { nom; prim; rapp } }
5548
| EOF { assert false }
5649

57-
correctif:
58-
ENTREESC
59-
ec = list(variable_and_value)
60-
CONTROLESC
61-
cc = list(variable_and_value)
62-
RESULTATSC
63-
rc = list(variable_and_value) { (ec, cc, rc) }
64-
50+
primitif:
51+
ENTREESPRIM
52+
entrees_primitif = list(variable_and_value)
53+
CONTROLESPRIM
54+
erreurs_attendues_primitif = list(error_code)
55+
RESULTATSPRIM
56+
resultats_attendus_primitif = list(variable_and_value)
57+
{ (entrees_primitif, erreurs_attendues_primitif, resultats_attendus_primitif) }
58+
59+
rappels:
60+
| ENTREESRAPP
61+
entrees_rappels = list(rappel)
62+
CONTROLESRAPP
63+
erreurs_attendues_rappels = list(error_code)
64+
RESULTATSRAPP
65+
resultats_attendus_rappels = list(variable_and_value)
66+
{ Some (entrees_rappels, erreurs_attendues_rappels, resultats_attendus_rappels) }
67+
| ENTREESCORR CONTROLESCORR RESULTATSCORR { None }
6568

6669
name:
6770
| n = NAME { n }
@@ -73,3 +76,24 @@ fip:
7376
variable_and_value:
7477
| var = SYMBOL SLASH value = INTEGER { (var, I (int_of_string value), mk_position $sloc) }
7578
| var = SYMBOL SLASH value = FLOAT { (var, F (float_of_string value), mk_position $sloc) }
79+
80+
error_code:
81+
error = SYMBOL { (error, mk_position $sloc) }
82+
83+
rappel:
84+
event_nb = INTEGER SLASH
85+
rappel_nb = INTEGER SLASH
86+
variable_change = variable_and_value SLASH
87+
direction = SYMBOL SLASH
88+
penalty_code = INTEGER SLASH
89+
base_tolerance_legale = INTEGER SLASH
90+
month_year = INTEGER SLASH
91+
decl_2042_rect = INTEGER
92+
{ (event_nb,
93+
rappel_nb,
94+
variable_change,
95+
direction,
96+
penalty_code,
97+
base_tolerance_legale,
98+
month_year,
99+
decl_2042_rect) }

examples/ocaml/parser/types_module.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ type literal = I of int | F of float
1414

1515
type var_values = (string * literal * t) list
1616

17-
type test_file = {
17+
type errors = (string * t) list
18+
19+
type rappels = (string * string * (string * literal * t) * string * string * string * string * string) list
20+
21+
type irj_file = {
1822
nom : string;
19-
ep : var_values;
20-
cp : var_values;
21-
rp : var_values;
22-
corr : (var_values * var_values * var_values) option;
23+
prim : (var_values * errors * var_values);
24+
rapp : (rappels * errors * var_values) option;
2325
}
2426

2527
(*For both lexer and parser, from m_frontend/parse_utils*)

examples/ocaml/test_harness.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ type test_block = { block_name : string; block_data : Mvalue.revenue_code list }
44

55
type test_data = { test_name : string; blocks : test_block list }
66

7-
let fichier (input_file : string) : Types_module.test_file = Fip.parse_file input_file
7+
let fichier (input_file : string) : Types_module.irj_file = Fip.parse_file input_file
88

99
let revenue_code_list_from_var_values (values : Types_module.var_values) : Mvalue.revenue_code list =
1010
List.map (fun (var, value, pos) -> match value with
1111
| Types_module.F value -> Mvalue.{alias=var; value=value }
1212
| Types_module.I _ -> assert false) values
1313

14-
let entry_list_parsed (fichier : Types_module.test_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.ep
14+
(*let entry_list_parsed (fichier : Types_module.irj_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.ep
1515
16-
let reference_list_parsed (fichier : Types_module.test_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.rp
16+
let reference_list_parsed (fichier : Types_module.irj_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.rp
17+
*)
1718

1819
let get_file_in_dir (dir_handle : Unix.dir_handle) : string list =
1920
let rec build_list file_list =
@@ -52,9 +53,10 @@ let print_rev_code (oc : Format.formatter) (rev_code : Mvalue.revenue_code) :
5253

5354
let compute_discrepancies_from_file_2020 (fip_file : string) :
5455
(Mvalue.revenue_code * Mvalue.revenue_code) list =
55-
let tax_result_array, errors = Ir.calculate_tax (entry_list_parsed (fichier fip_file)) in
56+
let (entry_list, _, result_list) = (fichier fip_file).prim in
57+
let tax_result_array, errors = Ir.calculate_tax (revenue_code_list_from_var_values entry_list) in
5658
let tax_result = Array.to_list tax_result_array in
57-
let ref_list = reference_list_parsed (fichier fip_file) in
59+
let ref_list = revenue_code_list_from_var_values result_list in
5860
let filtered_ref_list = filter_rev_code_list ref_list tax_result in
5961
let was_erased ref_list code : bool = not (List.mem code ref_list) in
6062
let erased_codes : revenue_code list =
@@ -133,16 +135,17 @@ let compute_on_FIP_2020 (fip_file : string) (output_file_name : string) : unit =
133135
Format.printf "Simple test on file %s.@." fip_file;
134136
let _oc = open_out (output_file_name ^ "_disc.txt") in
135137
let oc = Format.formatter_of_out_channel _oc in
138+
let (entry_list, _, result_list) = (fichier fip_file).prim in
136139
let tax_result_array, errors =
137-
try Ir.calculate_tax (entry_list_parsed (fichier fip_file))
140+
try Ir.calculate_tax (revenue_code_list_from_var_values entry_list)
138141
with M_exn e_list ->
139142
Format.fprintf oc "TEST CASE %s ends with M Exception:@,@,%a@." fip_file
140143
print_errors e_list;
141144
close_out _oc;
142145
raise (M_exn e_list)
143146
in
144147
let tax_result = Array.to_list tax_result_array in
145-
let ref_list = reference_list_parsed (fichier fip_file) in
148+
let ref_list = revenue_code_list_from_var_values result_list in
146149
let print_list fmt code_list =
147150
Format.pp_print_list print_rev_code fmt code_list
148151
in

0 commit comments

Comments
 (0)