Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IRJ files parser as an Mlang sublibrary #222

Merged
merged 19 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@
(= 0.19.0))
(parmap
(= 1.2.3))))

(package
(name irj_parser)
(synopsis "Parser for the IRJ tests")
(description "This parser is aimed for the tests used by la DGFiP to test the calculation of the French income tax")
(depends
(ocaml
(and(>= "4.11.2")))
(dune
(and :build))
(odoc
(= 1.5.3))
(ocamlformat
(= 0.19.0))))
15 changes: 6 additions & 9 deletions src/mlang/irj_parser/irj_parser.opam → irj_parser.opam
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "1.1.0"
synopsis: "Parser for the IRJ tests"
description:
"This parser is aimed for the tests used by la DGFiP to test the calculation of the French income tax"
maintainer: ["Mathieu Durero" " Noé Ensarguet"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ha ha why did you remove yourself and Mathieu and put me as the maintainer ;)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hahaha
I had a separate dune-project for irj_parser in the first commit, until I realised (thanks to @Keryan-dev) that you could declare both packages in the same dune-project file (and so with the same maintainers etc.)

authors: ["Mathieu Durero" "Raphaël Monat"]
maintainer: ["[email protected]"]
authors: ["Denis Merigoux" "Raphaël Monat"]
license: "GPL-3.0-or-later"
homepage: "https://github.com/MLanguage/mlang/tree/IRJ-parser-library"
bug-reports: "https://github.com/MLanguage/mlang/issues"
homepage: "https://gitlab.inria.fr/verifisc/mlang"
bug-reports: "https://gitlab.inria.fr/verifisc/mlang/issues"
depends: [
"ocaml" {>= "4.11.2"}
"dune" {build}
"ANSITerminal" {= "0.8.2"}
"cmdliner" {= "1.1.0"}
"re" {= "1.9.0"}
"ocamlgraph" {= "1.8.8"}
"odoc" {= "1.5.3"}
"dune-build-info" {= "2.5.1"}
"ocamlformat" {= "0.19.0"}
]
build: [
Expand All @@ -33,3 +29,4 @@ build: [
"@doc" {with-doc}
]
]
dev-repo: "git+https://gitlab.inria.fr/verifisc/mlang.git"
File renamed without changes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there this empty file here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thought we might want an executable at first and didn't delete it afterwards ^^'

File renamed without changes.
9 changes: 1 addition & 8 deletions src/mlang/irj_parser/src/dune → src/irj_parser/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
(ocamlopt_flags
(-O3 -ccopt -static))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto -O3


(include_subdirs unqualified)

(library
(name irj_file)
(public_name irj_parser)
(wrapped false)
(libraries ANSITerminal re dune-build-info threads cmdliner))
(wrapped false))

(ocamllex irj_lexer)

(menhir
(modules irj_parser)
(flags --strict --explain))

;(documentation
; (package irj_parser)
; (mld_files ("index")))
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>. *)

type t = { pos_filename : string; pos_loc : Lexing.position * Lexing.position }
Copy link
Contributor

Choose a reason for hiding this comment

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

That type should not be named t as twould be the main type of this module, which we expect to be the AST of a whole test file and not a code position.


let mk_position sloc =
{ pos_filename = (fst sloc).Lexing.pos_fname; pos_loc = sloc }

exception
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to duplicate the full type here as you won't use all its components in irj_parser. You can simply declare something like and later do raise TestParsingError(...).

exception TestParsingError of string * pos

StructuredError of (string * (string option * t) list * (unit -> unit) option)
(* duplication of some of the utils *)

type literal = I of int | F of float

type var_value = string * literal * Pos.t
type var_value = string * literal * t

(* type var_values = var_value list *)

type calc_error = string * Pos.t
type calc_error = string * t

(* type calc_errors = calc_error list *)

Expand All @@ -40,7 +49,7 @@ type rappel = {
(* MMYYYY *)
decl_2042_rect : int option;
(* 0 or 1 *)
pos : Pos.t;
pos : t;
}

type prim_data_block = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ let parse_file (test_name : string) : irj_file =
in
let f =
try Irj_parser.irj_file Irj_lexer.token filebuf with
| Errors.StructuredError e ->
| StructuredError e ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you can catch TestParsingError raised in the parser

close_in input;
raise (Errors.StructuredError e)
raise (StructuredError e)
| Irj_parser.Error ->
close_in input;
Errors.raise_spanned_error "Test syntax error"
(Parse_utils.mk_position (filebuf.lex_start_p, filebuf.lex_curr_p))
raise
(StructuredError
Copy link
Contributor

Choose a reason for hiding this comment

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

And here raise a TestParsingError

( "Test syntax error",
[ (None, mk_position (filebuf.lex_start_p, filebuf.lex_curr_p)) ],
None ))
in
close_in input;
f
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
{
open Lexing
open Irj_parser
open Irj_ast

let error lb msg =
raise (StructuredError ("Lexing error : " ^ msg,
[ (None, mk_position (Lexing.lexeme_start_p lb, Lexing.lexeme_end_p lb)) ],
None))

module StrMap = Map.Make (String)

Expand All @@ -40,11 +46,6 @@ let keywords =
"#AVIS_CSG", AVISCSG;
]

let error lb msg =
Errors.raise_spanned_error ("Lexing error : " ^ msg)
(Parse_utils.mk_position (Lexing.lexeme_start_p lb,
Lexing.lexeme_end_p lb))

let is_bol lb =
(* bol = beginning of line *)
lb.lex_start_p.pos_cnum - lb.lex_start_p.pos_bol = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
this program. If not, see <https://www.gnu.org/licenses/>. *)

%{ open Irj_ast

let error (sp, ep) msg =
Errors.raise_spanned_error ("Parse error : " ^ msg)
(Parse_utils.mk_position (sp, ep))
raise (StructuredError ("Parse error : " ^ msg, [ (None, mk_position (sp, ep)) ], None))
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you can raise a TestParsingError ...


%}

Expand Down Expand Up @@ -98,11 +97,11 @@ rappels:
| ENTREESCORR NL CONTROLESCORR NL RESULTATSCORR NL DATES? AVISIR? AVISCSG? { None }

variable_and_value:
| var = SYMBOL SLASH value = value NL { (var, value, Parse_utils.mk_position $sloc) }
| var = SYMBOL SLASH value = value NL { (var, value, mk_position $sloc) }
| SYMBOL error { error $loc "Missing slash in pair variable/value" }

calc_error:
error = SYMBOL NL { (error, Parse_utils.mk_position $sloc) }
error = SYMBOL NL { (error, mk_position $sloc) }

rappel:
event_nb = integer SLASH
Expand All @@ -129,7 +128,7 @@ rappel:
base_tolerance_legale;
month_year;
decl_2042_rect;
pos = Parse_utils.mk_position $sloc }
pos = mk_position $sloc }
}

integer:
Expand Down
46 changes: 0 additions & 46 deletions src/mlang/irj_parser/dune-project

This file was deleted.

41 changes: 24 additions & 17 deletions src/mlang/test_framework/test_interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

open Irj_ast

let convert_pos (sloc : Irj_ast.t) : Pos.t =
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes perfect!

{ pos_filename = sloc.pos_filename; pos_loc = sloc.pos_loc }

(* enforces type compatibility (the type t is defined in exactly the same way in
Irj_ast and in Pos) *)

let to_ast_literal (value : Irj_ast.literal) : Mast.literal =
match value with I i -> Float (float_of_int i) | F f -> Float f

Expand Down Expand Up @@ -43,7 +49,7 @@ let to_MIR_function_and_inputs (program : Bir.program) (t : irj_file)
List.fold_left
(fun (fv, in_f) (var, value, pos) ->
let var =
find_var_of_name program.mir_program (var, pos)
find_var_of_name program.mir_program (var, convert_pos pos)
|> Bir.(var_from_mir default_tgv)
in
let lit =
Expand All @@ -65,34 +71,35 @@ let to_MIR_function_and_inputs (program : Bir.program) (t : irj_file)
two using the line below*)
let var =
Pos.unmark
(find_var_of_name program.mir_program (var, pos))
(find_var_of_name program.mir_program (var, convert_pos pos))
.Mir.Variable.name
in
(* we allow a difference of 0.000001 between the control value and
the result *)
let first_exp =
( Mast.Comparison
( (Lte, pos),
( (Lte, convert_pos pos),
( Mast.Binop
( (Mast.Sub, pos),
(Literal (Variable (Normal var)), pos),
(Literal (to_ast_literal value), pos) ),
pos ),
(Literal (Float test_error_margin), pos) ),
pos )
( (Mast.Sub, convert_pos pos),
(Literal (Variable (Normal var)), convert_pos pos),
(Literal (to_ast_literal value), convert_pos pos) ),
convert_pos pos ),
(Literal (Float test_error_margin), convert_pos pos) ),
convert_pos pos )
in
let second_exp =
( Mast.Comparison
( (Lte, pos),
( (Lte, convert_pos pos),
( Mast.Binop
( (Mast.Sub, pos),
(Literal (to_ast_literal value), pos),
(Literal (Variable (Normal var)), pos) ),
pos ),
(Literal (Float test_error_margin), pos) ),
pos )
( (Mast.Sub, convert_pos pos),
(Literal (to_ast_literal value), convert_pos pos),
(Literal (Variable (Normal var)), convert_pos pos) ),
convert_pos pos ),
(Literal (Float test_error_margin), convert_pos pos) ),
convert_pos pos )
in
(Mast.Binop ((Mast.And, pos), first_exp, second_exp), pos))
( Mast.Binop ((Mast.And, convert_pos pos), first_exp, second_exp),
convert_pos pos ))
t.prim.resultats_attendus)
in
( { func_variable_inputs; func_constant_inputs; func_outputs; func_conds },
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

(** {2 Source code position} *)

type t
type t = { pos_filename : string; pos_loc : Lexing.position * Lexing.position }
(* opened this type. Not very satisfactory but necessary to enforce the
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of opening the type you can just use the make_position function below in test_interpreter.ml/convert_pos :)

compatibility of this type t and that of the module Irj_parser *)

val make_position : string -> Lexing.position * Lexing.position -> t

Expand Down
Loading