Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 23 additions & 20 deletions src/mlang/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ let parse () =
let filebuf = { filebuf with lex_curr_p } in
match Mparser.source_file token filebuf with
| commands -> commands
| exception Mparser.Error ->
Errors.raise_spanned_error "M syntax error"
(Parse_utils.mk_position (filebuf.lex_start_p, filebuf.lex_curr_p))
| exception Mparser.Error s -> (
let pos =
Parse_utils.mk_position (filebuf.lex_start_p, filebuf.lex_curr_p)
in
let err msg =
Format.kasprintf (fun m -> Errors.raise_spanned_error m pos) msg
in
match String.trim (Syntax_messages.message s) with
| exception Not_found -> err "Erreur de syntaxe innattendue."
| "<YOUR SYNTAX ERROR MESSAGE HERE>" -> err "Erreur de syntaxe %i" s
| msg -> err "Erreur: %s " msg)
in

let parse_file source_file =
Expand Down Expand Up @@ -159,23 +167,18 @@ let extract m_program =
| UnknownBackend -> Errors.raise_error "No backend specified!"

let driver () =
try
Cli.debug_print "Reading M files...";
let m_program = parse () in
Cli.debug_print "Elaborating...";
let m_program = Expander.proceed m_program in
let m_program = Validator.proceed !Config.mpp_function m_program in
let m_program = Mast_to_mir.translate m_program in
let m_program = Mir.expand_functions m_program in
Cli.debug_print "Creating combined program suitable for execution...";
match !Config.execution_mode with
| SingleTest test -> run_single_test m_program test
| MultipleTests tests -> run_multiple_tests m_program tests
| Extraction -> extract m_program
with Errors.StructuredError (msg, pos_list, kont) as e ->
Cli.error_print "%a" Errors.format_structured_error (msg, pos_list);
(match kont with None -> () | Some kont -> kont ());
raise e
Cli.debug_print "Reading M files...";
let m_program = parse () in
Cli.debug_print "Elaborating...";
let m_program = Expander.proceed m_program in
let m_program = Validator.proceed !Config.mpp_function m_program in
let m_program = Mast_to_mir.translate m_program in
let m_program = Mir.expand_functions m_program in
Cli.debug_print "Creating combined program suitable for execution...";
match !Config.execution_mode with
| SingleTest test -> run_single_test m_program test
| MultipleTests tests -> run_multiple_tests m_program tests
| Extraction -> extract m_program

let set_opts (files : string list) (application_names : string list)
(without_dgfip_m : bool) (debug : bool) (var_info_debug : string list)
Expand Down
3 changes: 2 additions & 1 deletion src/mlang/index.mld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ intermediate representations going from the source code to the target backend.
{1 Frontend}

First, the source code is parsed according to the Menhir grammar specified in {!module: Mlang.M_frontend.Mparser}.
The grammar is not exactly LR(1) so we rely on {!module: Mlang.M_frontend.Parse_utils} to backtrack, especially on symbol parsing. The target intermediate representation is {!module: Mlang.M_frontend.Mast}, which is very close to the concrete syntax and can be printed using {!module: Mlang.M_frontend.Format_mast}.
The grammar is not exactly LR(1) so we rely on {!module: Mlang.M_frontend.Parse_utils} to backtrack, especially on symbol parsing. {!module: Mlang.M_frontend.Syntax_messages} treats the error messages in the different failing cases. The target intermediate representation is {!module: Mlang.M_frontend.Mast}, which is very close to the concrete syntax and can be printed using {!module: Mlang.M_frontend.Format_mast}.
The frontend also handles ast expansion with {!module: Mlang.M_frontend.Expander} and validation with {!module: Mlang.M_frontend.Validator}.

{!modules:
Expand All @@ -15,6 +15,7 @@ The frontend also handles ast expansion with {!module: Mlang.M_frontend.Expander
Mlang.M_frontend.Mlexer
Mlang.M_frontend.Mparser
Mlang.M_frontend.Parse_utils
Mlang.M_frontend.Syntax_messages
Mlang.M_frontend.Validator }

{1 Intermediate Representation}
Expand Down
57 changes: 56 additions & 1 deletion src/mlang/m_frontend/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,66 @@

(menhir
(modules mparser)
(flags --explain))
(flags --exn-carries-state --explain))

(library
(public_name mlang.frontend)
(name m_frontend)
(flags
(:standard -open Utils -open M_ir))
(libraries utils m_ir))

; Generates the Syntax_messages module

(rule
(target syntax_messages.ml)
(deps
(:parser mparser.mly)
(:msg syntax.messages))
(action
(with-stdout-to
%{target}
(run menhir %{parser} --base %{parser} --compile-errors %{msg}))))

; Rules to check parser updates and properly merging the changes

(rule
(target new.messages)
(deps
(:parser mparser.mly))
(action
(with-stdout-to
%{target}
(run menhir %{parser} --base %{parser} --list-errors))))

(rule
(target updated.messages)
(deps
(:parser mparser.mly)
(:msg syntax.messages))
(action
(with-stdout-to
%{target}
(run menhir %{parser} --base %{parser} --update-errors %{msg}))))

(rule
(target syntax.messages.updated)
(deps
(:parser mparser.mly)
(:new new.messages)
(:updated updated.messages))
(action
(with-stdout-to
%{target}
(run menhir %{parser} --base %{parser} --merge-errors %{new}
--merge-errors %{updated}))))

; Tests the syntax.messages file when the parser changes.
; To update the file, use dune runtest --auto-update

(rule
(alias runtest)
(package mlang)
(deps syntax.messages syntax.messages.updated)
(action
(diff syntax.messages syntax.messages.updated)))
4 changes: 2 additions & 2 deletions src/mlang/m_frontend/mparser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
(** Module generated automaticcaly by Menhir, the parser generator *)
%}

%start source_file

%token<string> SYMBOL STRING

%token PLUS MINUS TIMES DIV MOD
Expand Down Expand Up @@ -68,8 +70,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
%nonassoc NOT
(* %nonassoc SYMBOL *)

%start source_file

%%

%inline with_pos(X):
Expand Down
Loading
Loading