2020-01-23 18:28:04 +01:00
|
|
|
(* Driver for the PascaLIGO parser *)
|
2019-05-12 20:56:22 +00:00
|
|
|
|
2020-01-04 19:49:22 +01:00
|
|
|
module IO =
|
|
|
|
struct
|
2020-04-03 19:08:14 +02:00
|
|
|
let options = EvalOpt.(read ~lang:`PascaLIGO ~ext:".ligo")
|
2020-01-04 19:49:22 +01:00
|
|
|
end
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module Parser =
|
2020-01-04 19:49:22 +01:00
|
|
|
struct
|
2020-01-14 01:27:35 +01:00
|
|
|
type ast = AST.t
|
2020-01-04 19:49:22 +01:00
|
|
|
type expr = AST.expr
|
|
|
|
include Parser
|
|
|
|
end
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module ParserLog =
|
2020-01-04 19:49:22 +01:00
|
|
|
struct
|
2020-01-14 01:27:35 +01:00
|
|
|
type ast = AST.t
|
|
|
|
type expr = AST.expr
|
2020-01-04 19:49:22 +01:00
|
|
|
include ParserLog
|
|
|
|
end
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module Lexer = Lexer.Make (LexToken)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
|
|
|
module Unit =
|
2020-01-14 01:27:35 +01:00
|
|
|
ParserUnit.Make (Lexer)(AST)(Parser)(ParErr)(ParserLog)(IO)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-04-03 19:08:14 +02:00
|
|
|
module SSet = Set.Make (String)
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
(* Main *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-23 18:28:04 +01:00
|
|
|
let issue_error error : ('a, string Region.reg) Stdlib.result =
|
|
|
|
Stdlib.Error (Unit.format_error ~offsets:IO.options#offsets
|
|
|
|
IO.options#mode error)
|
2020-01-14 01:27:35 +01:00
|
|
|
|
2020-01-23 18:28:04 +01:00
|
|
|
let parse parser : ('a, string Region.reg) Stdlib.result =
|
2020-01-14 01:27:35 +01:00
|
|
|
try parser () with
|
|
|
|
(* Scoping errors *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Scoping.Error (Scoping.Duplicate_parameter name) ->
|
2020-01-08 16:39:52 +01:00
|
|
|
let token =
|
2020-01-14 01:27:35 +01:00
|
|
|
Lexer.Token.mk_ident name.Region.value name.Region.region in
|
2020-04-01 19:22:46 +02:00
|
|
|
(match token with
|
2020-04-03 19:08:14 +02:00
|
|
|
(* Cannot fail because [name] is not a reserved name for the
|
|
|
|
lexer. *)
|
2020-04-01 19:22:46 +02:00
|
|
|
Stdlib.Error _ -> assert false
|
|
|
|
| Ok invalid ->
|
|
|
|
issue_error ("Duplicate parameter.\nHint: Change the name.\n",
|
|
|
|
None, invalid))
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Scoping.Error (Scoping.Reserved_name name) ->
|
2020-01-08 16:39:52 +01:00
|
|
|
let token =
|
2020-01-14 01:27:35 +01:00
|
|
|
Lexer.Token.mk_ident name.Region.value name.Region.region in
|
2020-01-08 16:39:52 +01:00
|
|
|
(match token with
|
2020-04-03 19:08:14 +02:00
|
|
|
(* Cannot fail because [name] is not a reserved name for the
|
|
|
|
lexer. *)
|
2020-01-14 01:27:35 +01:00
|
|
|
Stdlib.Error _ -> assert false
|
2020-01-08 16:39:52 +01:00
|
|
|
| Ok invalid ->
|
2020-01-14 01:27:35 +01:00
|
|
|
issue_error
|
|
|
|
("Reserved name.\nHint: Change the name.\n", None, invalid))
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Scoping.Error (Scoping.Duplicate_variant name) ->
|
2020-01-08 16:39:52 +01:00
|
|
|
let token =
|
2020-01-14 01:27:35 +01:00
|
|
|
Lexer.Token.mk_constr name.Region.value name.Region.region in
|
|
|
|
let point = "Duplicate constructor in this sum type declaration.\n\
|
|
|
|
Hint: Change the constructor.\n",
|
|
|
|
None, token
|
|
|
|
in issue_error point
|
2020-01-08 23:35:09 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Scoping.Error (Scoping.Non_linear_pattern var) ->
|
2020-01-08 23:35:09 +01:00
|
|
|
let token =
|
2020-01-14 01:27:35 +01:00
|
|
|
Lexer.Token.mk_ident var.Region.value var.Region.region in
|
2020-01-08 23:35:09 +01:00
|
|
|
(match token with
|
2020-04-03 19:08:14 +02:00
|
|
|
(* Cannot fail because [var] is not a reserved name for the
|
|
|
|
lexer. *)
|
2020-01-14 01:27:35 +01:00
|
|
|
Stdlib.Error _ -> assert false
|
2020-01-08 23:35:09 +01:00
|
|
|
| Ok invalid ->
|
2020-01-09 14:26:47 +01:00
|
|
|
let point = "Repeated variable in this pattern.\n\
|
|
|
|
Hint: Change the name.\n",
|
2020-01-14 01:27:35 +01:00
|
|
|
None, invalid
|
|
|
|
in issue_error point)
|
2020-01-09 14:26:47 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Scoping.Error (Scoping.Duplicate_field name) ->
|
2020-01-09 14:26:47 +01:00
|
|
|
let token =
|
2020-01-14 01:27:35 +01:00
|
|
|
Lexer.Token.mk_ident name.Region.value name.Region.region in
|
2020-01-09 14:26:47 +01:00
|
|
|
(match token with
|
2020-01-14 01:27:35 +01:00
|
|
|
(* Cannot fail because [name] is a not a
|
|
|
|
reserved name for the lexer. *)
|
|
|
|
Stdlib.Error _ -> assert false
|
2020-01-09 14:26:47 +01:00
|
|
|
| Ok invalid ->
|
2020-01-23 18:28:04 +01:00
|
|
|
let point =
|
|
|
|
"Duplicate field name in this record declaration.\n\
|
|
|
|
Hint: Change the name.\n",
|
|
|
|
None, invalid
|
|
|
|
in issue_error point)
|
|
|
|
|
2020-04-01 19:22:46 +02:00
|
|
|
(* Preprocessing the input source *)
|
2020-01-23 18:28:04 +01:00
|
|
|
|
2020-04-01 19:22:46 +02:00
|
|
|
let preproc cin : unit =
|
|
|
|
let close () = flush_all (); close_in cin in
|
|
|
|
let buffer = Lexing.from_channel cin in
|
|
|
|
let open Lexing in
|
|
|
|
let () =
|
|
|
|
match IO.options#input with
|
|
|
|
None | Some "-" -> ()
|
|
|
|
| Some pos_fname ->
|
|
|
|
buffer.lex_curr_p <- {buffer.lex_curr_p with pos_fname} in
|
|
|
|
match Preproc.lex IO.options buffer with
|
|
|
|
Stdlib.Error (pp_buffer, err) ->
|
2020-04-03 19:08:14 +02:00
|
|
|
if SSet.mem "preproc" IO.options#verbose then
|
2020-04-01 19:22:46 +02:00
|
|
|
Printf.printf "%s\n%!" (Buffer.contents pp_buffer);
|
|
|
|
let Region.{value; _} =
|
|
|
|
Preproc.format ~offsets:IO.options#offsets ~file:true err
|
|
|
|
in close (); Printf.eprintf "\027[31m%s\027[0m%!" value
|
|
|
|
| Stdlib.Ok pp_buffer ->
|
|
|
|
(* Running the lexer and the parser on the preprocessed input *)
|
|
|
|
|
|
|
|
let source = Lexer.String (Buffer.contents pp_buffer) in
|
|
|
|
match Lexer.open_token_stream source with
|
|
|
|
Stdlib.Ok instance ->
|
|
|
|
if IO.options#expr
|
|
|
|
then
|
|
|
|
match parse (fun () -> Unit.apply instance Unit.parse_expr) with
|
|
|
|
Stdlib.Ok _ -> ()
|
|
|
|
| Error Region.{value; _} ->
|
|
|
|
close (); Printf.eprintf "\027[31m%s\027[0m%!" value
|
|
|
|
else
|
|
|
|
(match parse (fun () -> Unit.apply instance Unit.parse_contract) with
|
|
|
|
Stdlib.Ok _ -> ()
|
|
|
|
| Error Region.{value; _} ->
|
|
|
|
close (); Printf.eprintf "\027[31m%s\027[0m%!" value)
|
|
|
|
| Stdlib.Error (Lexer.File_opening msg) ->
|
|
|
|
flush_all (); Printf.eprintf "\027[31m%s\027[0m%!" msg
|
2020-01-23 18:28:04 +01:00
|
|
|
|
2020-04-01 19:22:46 +02:00
|
|
|
let () =
|
|
|
|
match IO.options#input with
|
|
|
|
Some "-" | None -> preproc stdin
|
|
|
|
| Some file_path ->
|
|
|
|
try open_in file_path |> preproc with
|
|
|
|
Sys_error msg ->
|
|
|
|
(flush_all (); Printf.eprintf "\027[31m%s\027[0m%!" msg)
|