Skip to content

Commit

Permalink
Report work on parser from ocaml-backend branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mdurero committed Jul 5, 2023
1 parent 17a5ce9 commit 76d5262
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/mlang/irj_parser/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ocamllex irj_lexer)

(menhir
(modules irj_parser))
19 changes: 19 additions & 0 deletions src/mlang/irj_parser/irj_ast.ml
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions src/mlang/irj_parser/irj_file.ml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions src/mlang/irj_parser/irj_file.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(* Copyright Inria, contributors: Raphaël Monat <[email protected]> (2019)
Mathieu Durero <[email protected]> (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 <https://www.gnu.org/licenses/>. *)

val parse_file : string -> Irj_ast.irj_file
(** [parse_file file] loads the content of a given IRJ [file] in a simple
datastructure. *)
74 changes: 74 additions & 0 deletions src/mlang/irj_parser/irj_lexer.mll
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(*
Copyright Inria, contributors:
Raphaël Monat <[email protected]> (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 <https://www.gnu.org/licenses/>.
*)

{
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)) }
99 changes: 99 additions & 0 deletions src/mlang/irj_parser/irj_parser.mly
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(*
Copyright Inria, contributors:
Raphaël Monat <[email protected]> (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 <https://www.gnu.org/licenses/>.
*)

%{ open Irj_ast
%}

%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 ENTREESPRIM CONTROLESPRIM RESULTATSPRIM
%token ENTREESCORR CONTROLESCORR RESULTATSCORR
%token ENTREESRAPP CONTROLESRAPP RESULTATSRAPP
/* %token DATES AVISIR AVISCSG*/
%token ENDSHARP

%token EOF

%type<irj_file> 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) }

0 comments on commit 76d5262

Please sign in to comment.