Skip to content

Commit

Permalink
Refine an independent IRJ test files parser. Compatibility with both …
Browse files Browse the repository at this point in the history
…primary and corrective test files.
  • Loading branch information
mdurero committed Jul 6, 2023
1 parent d09444b commit cd23ed2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 51 deletions.
23 changes: 19 additions & 4 deletions examples/ocaml/parser/fip.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(*From test_interpreter.ml*)

let parse_file (test_name : string) : Types_module.test_file =
open Types_module

let parse_file (test_name : string) : Types_module.irj_file =
let input = open_in test_name in
let filebuf = Lexing.from_channel input in
let filebuf =
Expand All @@ -10,14 +12,27 @@ let parse_file (test_name : string) : Types_module.test_file =
}
in
let f =
try Test_parser.test_file Test_lexer.token filebuf with
try Test_parser.irj_file Test_lexer.token filebuf with
| Types_module.StructuredError e ->
close_in input;
raise (Types_module.StructuredError e)
| Test_parser.Error ->
close_in input;
Types_module.raise_spanned_error "Test syntax error"
(Types_module.mk_position (filebuf.lex_start_p, filebuf.lex_curr_p))
in
in
close_in input;
f
f
(*
let () =
let donnees = parse_file "alavoine-2.irj" in
let (entrees, _, _) = donnees.prim in
print_string donnees.nom;print_newline();
let (code, valeur, _) = List.hd entrees in
print_string "première entrée : "; print_string code; print_string " avec la valeur ";
match valeur with
| I entier -> print_int entier; print_string " entière.";print_newline();flush stdout
| F flottant -> print_float flottant; print_string " flottante.";
print_newline();
flush stdout
*)
22 changes: 14 additions & 8 deletions examples/ocaml/parser/test_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,29 @@ rule token = parse
| "#FIP"
{ FIP }
| "#ENTREES-PRIMITIF"
{ ENTREESP }
{ ENTREESPRIM }
| "#CONTROLES-PRIMITIF"
{ CONTROLESP }
{ CONTROLESPRIM }
| "#RESULTATS-PRIMITIF"
{ RESULTATSP }
{ RESULTATSPRIM }
| "#ENTREES-CORRECTIF"
{ ENTREESC }
{ ENTREESCORR }
| "#CONTROLES-CORRECTIF"
{ CONTROLESC }
{ CONTROLESCORR }
| "#RESULTATS-CORRECTIF"
{ RESULTATSC }
| "#DATES"
{ RESULTATSCORR }
| "#ENTREES-RAPPELS"
{ ENTREESRAPP }
| "#CONTROLES-RAPPELS"
{ CONTROLESRAPP }
| "#RESULTATS-RAPPELS"
{ RESULTATSRAPP }
(*| "#DATES"
{ DATES }
| "#AVIS_IR"
{ AVISIR }
| "#AVIS_CSG"
{ AVISCSG }
{ AVISCSG }*)
| "##"
{ ENDSHARP }
| '-'? ['0' - '9']+ as i
Expand Down
78 changes: 51 additions & 27 deletions examples/ocaml/parser/test_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,51 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
%}

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

%token SLASH
%token NOM FIP
%token ENTREESP CONTROLESP RESULTATSP
%token ENTREESC CONTROLESC RESULTATSC
%token DATES AVISIR AVISCSG
%token ENTREESPRIM CONTROLESPRIM RESULTATSPRIM
%token ENTREESCORR CONTROLESCORR RESULTATSCORR
%token ENTREESRAPP CONTROLESRAPP RESULTATSRAPP
/* %token DATES AVISIR AVISCSG*/
%token ENDSHARP

%token EOF

%type<Types_module.test_file> test_file
%type<Types_module.irj_file> irj_file

%start test_file
%start irj_file

%%



test_file:
irj_file:
| NOM nom = name
fip?
ENTREESP
ep = list(variable_and_value)
CONTROLESP
cp = list(variable_and_value)
RESULTATSP
rp = list(variable_and_value)
corr = correctif?
DATES?
AVISIR?
AVISCSG?
ENDSHARP { { nom; ep; cp; rp; corr } }
prim = primitif
rapp = rappels
ENDSHARP { { nom; prim; rapp } }
| EOF { assert false }

correctif:
ENTREESC
ec = list(variable_and_value)
CONTROLESC
cc = list(variable_and_value)
RESULTATSC
rc = list(variable_and_value) { (ec, cc, rc) }

primitif:
ENTREESPRIM
entrees_primitif = list(variable_and_value)
CONTROLESPRIM
erreurs_attendues_primitif = list(error_code)
RESULTATSPRIM
resultats_attendus_primitif = list(variable_and_value)
{ (entrees_primitif, erreurs_attendues_primitif, resultats_attendus_primitif) }

rappels:
| ENTREESRAPP
entrees_rappels = list(rappel)
CONTROLESRAPP
erreurs_attendues_rappels = list(error_code)
RESULTATSRAPP
resultats_attendus_rappels = list(variable_and_value)
{ Some (entrees_rappels, erreurs_attendues_rappels, resultats_attendus_rappels) }
| ENTREESCORR CONTROLESCORR RESULTATSCORR { None }

name:
| n = NAME { n }
Expand All @@ -73,3 +76,24 @@ fip:
variable_and_value:
| var = SYMBOL SLASH value = INTEGER { (var, I (int_of_string value), mk_position $sloc) }
| var = SYMBOL SLASH value = FLOAT { (var, F (float_of_string value), mk_position $sloc) }

error_code:
error = SYMBOL { (error, mk_position $sloc) }

rappel:
event_nb = INTEGER SLASH
rappel_nb = INTEGER SLASH
variable_change = variable_and_value SLASH
direction = SYMBOL SLASH
penalty_code = INTEGER SLASH
base_tolerance_legale = INTEGER SLASH
month_year = INTEGER SLASH
decl_2042_rect = INTEGER
{ (event_nb,
rappel_nb,
variable_change,
direction,
penalty_code,
base_tolerance_legale,
month_year,
decl_2042_rect) }
12 changes: 7 additions & 5 deletions examples/ocaml/parser/types_module.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type literal = I of int | F of float

type var_values = (string * literal * t) list

type test_file = {
type errors = (string * t) list

type rappels = (string * string * (string * literal * t) * string * string * string * string * string) list

type irj_file = {
nom : string;
ep : var_values;
cp : var_values;
rp : var_values;
corr : (var_values * var_values * var_values) option;
prim : (var_values * errors * var_values);
rapp : (rappels * errors * var_values) option;
}

(*For both lexer and parser, from m_frontend/parse_utils*)
Expand Down
17 changes: 10 additions & 7 deletions examples/ocaml/test_harness.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ type test_block = { block_name : string; block_data : Mvalue.revenue_code list }

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

let fichier (input_file : string) : Types_module.test_file = Fip.parse_file input_file
let fichier (input_file : string) : Types_module.irj_file = Fip.parse_file input_file

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

let entry_list_parsed (fichier : Types_module.test_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.ep
(*let entry_list_parsed (fichier : Types_module.irj_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.ep
let reference_list_parsed (fichier : Types_module.test_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.rp
let reference_list_parsed (fichier : Types_module.irj_file) : Mvalue.revenue_code list = revenue_code_list_from_var_values fichier.rp
*)

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

let compute_discrepancies_from_file_2020 (fip_file : string) :
(Mvalue.revenue_code * Mvalue.revenue_code) list =
let tax_result_array, errors = Ir.calculate_tax (entry_list_parsed (fichier fip_file)) in
let (entry_list, _, result_list) = (fichier fip_file).prim in
let tax_result_array, errors = Ir.calculate_tax (revenue_code_list_from_var_values entry_list) in
let tax_result = Array.to_list tax_result_array in
let ref_list = reference_list_parsed (fichier fip_file) in
let ref_list = revenue_code_list_from_var_values result_list in
let filtered_ref_list = filter_rev_code_list ref_list tax_result in
let was_erased ref_list code : bool = not (List.mem code ref_list) in
let erased_codes : revenue_code list =
Expand Down Expand Up @@ -133,16 +135,17 @@ let compute_on_FIP_2020 (fip_file : string) (output_file_name : string) : unit =
Format.printf "Simple test on file %s.@." fip_file;
let _oc = open_out (output_file_name ^ "_disc.txt") in
let oc = Format.formatter_of_out_channel _oc in
let (entry_list, _, result_list) = (fichier fip_file).prim in
let tax_result_array, errors =
try Ir.calculate_tax (entry_list_parsed (fichier fip_file))
try Ir.calculate_tax (revenue_code_list_from_var_values entry_list)
with M_exn e_list ->
Format.fprintf oc "TEST CASE %s ends with M Exception:@,@,%a@." fip_file
print_errors e_list;
close_out _oc;
raise (M_exn e_list)
in
let tax_result = Array.to_list tax_result_array in
let ref_list = reference_list_parsed (fichier fip_file) in
let ref_list = revenue_code_list_from_var_values result_list in
let print_list fmt code_list =
Format.pp_print_list print_rev_code fmt code_list
in
Expand Down

0 comments on commit cd23ed2

Please sign in to comment.