2019-12-20 19:44:03 +04:00
|
|
|
(** Driver for the CameLIGO parser *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2020-01-04 22:49:22 +04:00
|
|
|
module IO =
|
|
|
|
struct
|
|
|
|
let ext = ".mligo"
|
|
|
|
let options = EvalOpt.read "CameLIGO" ext
|
|
|
|
end
|
|
|
|
|
2020-01-14 04:27:35 +04:00
|
|
|
module Parser =
|
2020-01-04 22:49:22 +04:00
|
|
|
struct
|
2020-01-14 04:27:35 +04:00
|
|
|
type ast = AST.t
|
2020-01-04 22:49:22 +04:00
|
|
|
type expr = AST.expr
|
|
|
|
include Parser
|
|
|
|
end
|
|
|
|
|
2020-01-14 04:27:35 +04:00
|
|
|
module ParserLog =
|
2020-01-04 22:49:22 +04:00
|
|
|
struct
|
2020-01-14 04:27:35 +04:00
|
|
|
type ast = AST.t
|
|
|
|
type expr = AST.expr
|
2020-01-04 22:49:22 +04:00
|
|
|
include ParserLog
|
|
|
|
end
|
|
|
|
|
2020-01-14 04:27:35 +04:00
|
|
|
module Lexer = Lexer.Make (LexToken)
|
2020-01-08 19:39:52 +04:00
|
|
|
|
|
|
|
module Unit =
|
2020-01-14 04:27:35 +04:00
|
|
|
ParserUnit.Make (Lexer)(AST)(Parser)(ParErr)(ParserLog)(IO)
|
|
|
|
|
|
|
|
(* Main *)
|
|
|
|
|
|
|
|
let issue_error point =
|
|
|
|
let error = Unit.format_error ~offsets:IO.options#offsets
|
|
|
|
IO.options#mode point
|
2020-01-14 21:35:30 +04:00
|
|
|
in Stdlib.Error error
|
2020-01-14 04:27:35 +04:00
|
|
|
|
2020-01-14 21:35:30 +04:00
|
|
|
let parse parser : ('a,string) Stdlib.result =
|
2020-01-14 04:27:35 +04:00
|
|
|
try parser () with
|
|
|
|
(* Scoping errors *)
|
|
|
|
|
|
|
|
| Scoping.Error (Scoping.Reserved_name name) ->
|
|
|
|
let token =
|
|
|
|
Lexer.Token.mk_ident name.Region.value name.Region.region in
|
|
|
|
(match token with
|
|
|
|
(* Cannot fail because [name] is a not a
|
|
|
|
reserved name for the lexer. *)
|
|
|
|
Stdlib.Error _ -> assert false
|
|
|
|
| Ok invalid ->
|
|
|
|
issue_error
|
|
|
|
("Reserved name.\nHint: Change the name.\n", None, invalid))
|
|
|
|
|
|
|
|
| Scoping.Error (Scoping.Duplicate_variant name) ->
|
|
|
|
let token =
|
|
|
|
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
|
|
|
|
|
|
|
|
| Scoping.Error (Scoping.Non_linear_pattern var) ->
|
|
|
|
let token =
|
|
|
|
Lexer.Token.mk_ident var.Region.value var.Region.region in
|
|
|
|
(match token with
|
|
|
|
(* Cannot fail because [var] is a not a
|
|
|
|
reserved name for the lexer. *)
|
|
|
|
Stdlib.Error _ -> assert false
|
|
|
|
| Ok invalid ->
|
|
|
|
let point = "Repeated variable in this pattern.\n\
|
|
|
|
Hint: Change the name.\n",
|
|
|
|
None, invalid
|
|
|
|
in issue_error point)
|
|
|
|
|
|
|
|
| Scoping.Error (Scoping.Duplicate_field name) ->
|
|
|
|
let token =
|
|
|
|
Lexer.Token.mk_ident name.Region.value name.Region.region in
|
|
|
|
(match token with
|
|
|
|
(* Cannot fail because [name] is a not a
|
|
|
|
reserved name for the lexer. *)
|
|
|
|
Stdlib.Error _ -> assert false
|
|
|
|
| Ok invalid ->
|
|
|
|
let point = "Duplicate field name in this record declaration.\n\
|
|
|
|
Hint: Change the name.\n",
|
|
|
|
None, invalid
|
|
|
|
in issue_error point)
|
2020-01-08 19:39:52 +04:00
|
|
|
|
2020-01-14 04:27:35 +04:00
|
|
|
let () =
|
|
|
|
if IO.options#expr
|
|
|
|
then match parse (fun () -> Unit.parse Unit.parse_expr) with
|
|
|
|
Stdlib.Ok _ -> ()
|
|
|
|
| Error msg -> Printf.eprintf "\027[31m%s\027[0m%!" msg
|
|
|
|
else match parse (fun () -> Unit.parse Unit.parse_contract) with
|
|
|
|
Stdlib.Ok _ -> ()
|
|
|
|
| Error msg -> Printf.eprintf "\027[31m%s\027[0m%!" msg
|