From 76d5262574d18491fd3d4ac0c4ca21a8e510a2c8 Mon Sep 17 00:00:00 2001 From: Mathieu Durero Date: Wed, 5 Jul 2023 14:19:39 +0200 Subject: [PATCH] Report work on parser from ocaml-backend branch --- src/mlang/irj_parser/dune | 4 ++ src/mlang/irj_parser/irj_ast.ml | 19 ++++++ src/mlang/irj_parser/irj_file.ml | 25 ++++++++ src/mlang/irj_parser/irj_file.mli | 19 ++++++ src/mlang/irj_parser/irj_lexer.mll | 74 +++++++++++++++++++++ src/mlang/irj_parser/irj_parser.mly | 99 +++++++++++++++++++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 src/mlang/irj_parser/dune create mode 100644 src/mlang/irj_parser/irj_ast.ml create mode 100644 src/mlang/irj_parser/irj_file.ml create mode 100644 src/mlang/irj_parser/irj_file.mli create mode 100644 src/mlang/irj_parser/irj_lexer.mll create mode 100644 src/mlang/irj_parser/irj_parser.mly diff --git a/src/mlang/irj_parser/dune b/src/mlang/irj_parser/dune new file mode 100644 index 000000000..fc40da011 --- /dev/null +++ b/src/mlang/irj_parser/dune @@ -0,0 +1,4 @@ +(ocamllex irj_lexer) + +(menhir + (modules irj_parser)) \ No newline at end of file diff --git a/src/mlang/irj_parser/irj_ast.ml b/src/mlang/irj_parser/irj_ast.ml new file mode 100644 index 000000000..f1ba43c08 --- /dev/null +++ b/src/mlang/irj_parser/irj_ast.ml @@ -0,0 +1,19 @@ +(*From test_ast.ml*) + +type literal = I of int | F of float + +type var_value = string * literal * Pos.t + +type var_values = var_value list + +type errors = (string * Pos.t) list + +type rappels = + (string * string * var_value * string * string * string * string * string) + list + +type irj_file = { + nom : string; + prim : var_values * errors * var_values; + rapp : (rappels * errors * var_values) option; +} \ No newline at end of file diff --git a/src/mlang/irj_parser/irj_file.ml b/src/mlang/irj_parser/irj_file.ml new file mode 100644 index 000000000..a7a23cb97 --- /dev/null +++ b/src/mlang/irj_parser/irj_file.ml @@ -0,0 +1,25 @@ +(*From test_interpreter.ml*) + +open Irj_ast + +let parse_file (test_name : string) : irj_file = + let input = open_in test_name in + let filebuf = Lexing.from_channel input in + let filebuf = + { + filebuf with + lex_curr_p = { filebuf.lex_curr_p with pos_fname = test_name }; + } + in + let f = + try Irj_parser.irj_file Irj_lexer.token filebuf with + | Errors.StructuredError e -> + close_in input; + raise (Errors.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)) + in + close_in input; + f diff --git a/src/mlang/irj_parser/irj_file.mli b/src/mlang/irj_parser/irj_file.mli new file mode 100644 index 000000000..c0dab0c7f --- /dev/null +++ b/src/mlang/irj_parser/irj_file.mli @@ -0,0 +1,19 @@ +(* Copyright Inria, contributors: Raphaël Monat (2019) + Mathieu Durero (2023) + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + this program. If not, see . *) + +val parse_file : string -> Irj_ast.irj_file +(** [parse_file file] loads the content of a given IRJ [file] in a simple + datastructure. *) diff --git a/src/mlang/irj_parser/irj_lexer.mll b/src/mlang/irj_parser/irj_lexer.mll new file mode 100644 index 000000000..255f59e19 --- /dev/null +++ b/src/mlang/irj_parser/irj_lexer.mll @@ -0,0 +1,74 @@ +(* +Copyright Inria, contributors: + Raphaël Monat (2019) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*) + +{ +open Lexing +open Irj_parser +} + +rule token = parse +| [' ' '\t'] (* also ignore newlines, not only whitespace and tabs *) + { token lexbuf } +| '*' [^ '\n']* '\n' (* ignore comments *) + { new_line lexbuf; token lexbuf } +| '\n' | "\r\n" + { new_line lexbuf; token lexbuf} +| "/" + { SLASH } +| "#NOM" + { NOM } +| "#FIP" + { FIP } +| "#ENTREES-PRIMITIF" + { ENTREESPRIM } +| "#CONTROLES-PRIMITIF" + { CONTROLESPRIM } +| "#RESULTATS-PRIMITIF" + { RESULTATSPRIM } +| "#ENTREES-CORRECTIF" + { ENTREESCORR } +| "#CONTROLES-CORRECTIF" + { CONTROLESCORR } +| "#RESULTATS-CORRECTIF" + { RESULTATSCORR } +| "#ENTREES-RAPPELS" + { ENTREESRAPP } +| "#CONTROLES-RAPPELS" + { CONTROLESRAPP } +| "#RESULTATS-RAPPELS" + { RESULTATSRAPP } +(*| "#DATES" + { DATES } +| "#AVIS_IR" + { AVISIR } +| "#AVIS_CSG" + { AVISCSG }*) +| "##" + { ENDSHARP } +| '-'? ['0' - '9']+ as i + { INTEGER i } +| '-'? ['0' - '9']+ '.' ['0' - '9']* as f + { FLOAT f } +| ['a'-'z' 'A'-'Z' '0'-'9' '_']+ as s + { SYMBOL s } +| ['a'-'z' 'A'-'Z' ' ' '0'-'9' ';' '-']+ as s + { NAME s } +| eof + { EOF } +| _ + { Errors.raise_spanned_error "Test file lexer error" (Parse_utils.mk_position (Lexing.lexeme_start_p lexbuf, Lexing.lexeme_end_p lexbuf)) } diff --git a/src/mlang/irj_parser/irj_parser.mly b/src/mlang/irj_parser/irj_parser.mly new file mode 100644 index 000000000..eff8f4cf7 --- /dev/null +++ b/src/mlang/irj_parser/irj_parser.mly @@ -0,0 +1,99 @@ +(* +Copyright Inria, contributors: + Raphaël Monat (2019) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*) + +%{ open Irj_ast +%} + +%token 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 ENTREESPRIM CONTROLESPRIM RESULTATSPRIM +%token ENTREESCORR CONTROLESCORR RESULTATSCORR +%token ENTREESRAPP CONTROLESRAPP RESULTATSRAPP +/* %token DATES AVISIR AVISCSG*/ +%token ENDSHARP + +%token EOF + +%type irj_file + +%start irj_file + +%% + +irj_file: +| NOM nom = name + fip? + prim = primitif + rapp = rappels + ENDSHARP { { nom; prim; rapp } } +| EOF { assert false } + +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 } +| n = SYMBOL { n } + +fip: + FIP SLASH option(SYMBOL) { } + +variable_and_value: +| var = SYMBOL SLASH value = INTEGER { (var, I (int_of_string value), Parse_utils.mk_position $sloc) } +| var = SYMBOL SLASH value = FLOAT { (var, F (float_of_string value), Parse_utils.mk_position $sloc) } + +error_code: + error = SYMBOL { (error, Parse_utils.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) }