2020-01-04 23:32:50 +01:00
|
|
|
(* Functor to build a standalone LIGO parser *)
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module type IO =
|
2020-01-04 23:32:50 +01:00
|
|
|
sig
|
|
|
|
val ext : string (* LIGO file extension *)
|
|
|
|
val options : EvalOpt.options (* CLI options *)
|
|
|
|
end
|
|
|
|
|
|
|
|
module type Pretty =
|
|
|
|
sig
|
|
|
|
type state
|
|
|
|
type ast
|
2020-01-14 01:27:35 +01:00
|
|
|
type expr
|
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
val mk_state :
|
|
|
|
offsets:bool -> mode:[`Point|`Byte] -> buffer:Buffer.t -> state
|
2020-01-14 01:27:35 +01:00
|
|
|
|
|
|
|
val pp_ast : state -> ast -> unit
|
|
|
|
val pp_expr : state -> expr -> unit
|
|
|
|
val print_tokens : state -> ast -> unit
|
|
|
|
val print_expr : state -> expr -> unit
|
2020-01-04 23:32:50 +01:00
|
|
|
end
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module Make (Lexer: Lexer.S)
|
2020-01-04 23:32:50 +01:00
|
|
|
(AST: sig type t type expr end)
|
|
|
|
(Parser: ParserAPI.PARSER
|
|
|
|
with type ast = AST.t
|
|
|
|
and type expr = AST.expr
|
|
|
|
and type token = Lexer.token)
|
|
|
|
(ParErr: sig val message : int -> string end)
|
2020-01-14 01:27:35 +01:00
|
|
|
(ParserLog: Pretty with type ast = AST.t
|
|
|
|
and type expr = AST.expr)
|
|
|
|
(IO: IO) =
|
2020-01-04 23:32:50 +01:00
|
|
|
struct
|
|
|
|
open Printf
|
|
|
|
|
|
|
|
(* Error printing and exception tracing *)
|
|
|
|
|
|
|
|
let () = Printexc.record_backtrace true
|
|
|
|
|
|
|
|
let external_ text =
|
|
|
|
Utils.highlight (sprintf "External error: %s" text); exit 1
|
|
|
|
|
|
|
|
(* Extracting the input file *)
|
|
|
|
|
|
|
|
let file =
|
|
|
|
match IO.options#input with
|
|
|
|
None | Some "-" -> false
|
|
|
|
| Some _ -> true
|
|
|
|
|
|
|
|
(* Preprocessing the input source and opening the input channels *)
|
|
|
|
|
|
|
|
(* Path for CPP inclusions (#include) *)
|
|
|
|
|
|
|
|
let lib_path =
|
|
|
|
match IO.options#libs with
|
|
|
|
[] -> ""
|
|
|
|
| libs -> let mk_I dir path = sprintf " -I %s%s" dir path
|
|
|
|
in List.fold_right mk_I libs ""
|
|
|
|
|
|
|
|
let prefix =
|
|
|
|
match IO.options#input with
|
|
|
|
None | Some "-" -> "temp"
|
2020-01-14 01:27:35 +01:00
|
|
|
| Some file -> Filename.(file |> basename |> remove_extension)
|
2020-01-04 23:32:50 +01:00
|
|
|
|
|
|
|
let suffix = ".pp" ^ IO.ext
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module SSet = Utils.String.Set
|
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
let pp_input =
|
2020-01-14 01:27:35 +01:00
|
|
|
if SSet.mem "cpp" IO.options#verbose
|
2020-01-04 23:32:50 +01:00
|
|
|
then prefix ^ suffix
|
2020-01-14 01:27:35 +01:00
|
|
|
else let pp_input, pp_out =
|
|
|
|
Filename.open_temp_file prefix suffix
|
2020-01-04 23:32:50 +01:00
|
|
|
in close_out pp_out; pp_input
|
|
|
|
|
|
|
|
let cpp_cmd =
|
|
|
|
match IO.options#input with
|
|
|
|
None | Some "-" ->
|
|
|
|
sprintf "cpp -traditional-cpp%s - > %s"
|
|
|
|
lib_path pp_input
|
|
|
|
| Some file ->
|
|
|
|
sprintf "cpp -traditional-cpp%s %s > %s"
|
|
|
|
lib_path file pp_input
|
|
|
|
|
|
|
|
let () =
|
2020-01-14 01:27:35 +01:00
|
|
|
if SSet.mem "cpp" IO.options#verbose
|
2020-01-04 23:32:50 +01:00
|
|
|
then eprintf "%s\n%!" cpp_cmd;
|
|
|
|
if Sys.command cpp_cmd <> 0 then
|
|
|
|
external_ (sprintf "the command \"%s\" failed." cpp_cmd)
|
|
|
|
|
|
|
|
(* Instanciating the lexer *)
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
module ParserFront = ParserAPI.Make (Lexer)(Parser)(ParErr)
|
2020-01-04 23:32:50 +01:00
|
|
|
|
2020-01-08 16:39:52 +01:00
|
|
|
let format_error = ParserFront.format_error
|
2020-01-14 01:27:35 +01:00
|
|
|
let short_error = ParserFront.short_error
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
let lexer_inst = Lexer.open_token_stream (Some pp_input)
|
2020-01-14 01:27:35 +01:00
|
|
|
let Lexer.{read; buffer; get_win; close; _} = lexer_inst
|
2020-01-04 23:32:50 +01:00
|
|
|
|
|
|
|
and cout = stdout
|
|
|
|
|
|
|
|
let close_all () = close (); close_out cout
|
|
|
|
|
|
|
|
(* Tokeniser *)
|
|
|
|
|
|
|
|
module Log = LexerLog.Make (Lexer)
|
|
|
|
|
|
|
|
let log = Log.output_token ~offsets:IO.options#offsets
|
|
|
|
IO.options#mode IO.options#cmd cout
|
|
|
|
|
|
|
|
let tokeniser = read ~log
|
|
|
|
|
|
|
|
(* Main *)
|
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
let output = Buffer.create 131
|
|
|
|
let state = ParserLog.mk_state
|
|
|
|
~offsets:IO.options#offsets
|
|
|
|
~mode:IO.options#mode
|
|
|
|
~buffer:output
|
|
|
|
|
|
|
|
(* Parsing an expression *)
|
|
|
|
|
|
|
|
let parse_expr () : AST.expr =
|
|
|
|
let expr =
|
|
|
|
if IO.options#mono then
|
|
|
|
ParserFront.mono_expr tokeniser buffer
|
|
|
|
else
|
|
|
|
ParserFront.incr_expr lexer_inst in
|
|
|
|
let () =
|
|
|
|
if SSet.mem "ast-tokens" IO.options#verbose
|
|
|
|
then begin
|
|
|
|
Buffer.clear output;
|
|
|
|
ParserLog.print_expr state expr;
|
|
|
|
Buffer.output_buffer stdout output
|
|
|
|
end in
|
|
|
|
let () =
|
|
|
|
if SSet.mem "ast" IO.options#verbose
|
|
|
|
then begin
|
|
|
|
Buffer.clear output;
|
|
|
|
ParserLog.pp_expr state expr;
|
|
|
|
Buffer.output_buffer stdout output
|
2020-01-04 23:32:50 +01:00
|
|
|
end
|
2020-01-14 01:27:35 +01:00
|
|
|
in expr (* Or more CLI options handled before *)
|
|
|
|
|
|
|
|
(* Parsing a contract *)
|
|
|
|
|
|
|
|
let parse_contract () : AST.t =
|
|
|
|
let ast =
|
|
|
|
if IO.options#mono then
|
|
|
|
ParserFront.mono_contract tokeniser buffer
|
|
|
|
else
|
|
|
|
ParserFront.incr_contract lexer_inst in
|
|
|
|
let () =
|
|
|
|
if SSet.mem "ast" IO.options#verbose
|
|
|
|
then begin
|
|
|
|
Buffer.clear output;
|
|
|
|
ParserLog.pp_ast state ast;
|
|
|
|
Buffer.output_buffer stdout output
|
|
|
|
end in
|
|
|
|
let () =
|
|
|
|
if SSet.mem "ast-tokens" IO.options#verbose
|
|
|
|
then begin
|
|
|
|
Buffer.clear output;
|
2020-01-04 23:32:50 +01:00
|
|
|
ParserLog.print_tokens state ast;
|
2020-01-14 01:27:35 +01:00
|
|
|
Buffer.output_buffer stdout output
|
2020-01-04 23:32:50 +01:00
|
|
|
end
|
2020-01-14 01:27:35 +01:00
|
|
|
in ast (* Or more CLI options handled before. *)
|
|
|
|
|
|
|
|
let parse (parser: unit -> 'a) : ('a,string) Stdlib.result =
|
|
|
|
try
|
|
|
|
let node = parser () in (close_all (); Ok node)
|
2020-01-04 23:32:50 +01:00
|
|
|
with
|
|
|
|
(* Lexing errors *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
Lexer.Error err ->
|
2020-01-14 01:27:35 +01:00
|
|
|
let error =
|
2020-01-08 16:39:52 +01:00
|
|
|
Lexer.format_error ~offsets:IO.options#offsets
|
|
|
|
IO.options#mode err ~file
|
2020-01-14 01:27:35 +01:00
|
|
|
in close_all (); Stdlib.Error error
|
2020-01-04 23:32:50 +01:00
|
|
|
|
|
|
|
(* Incremental API of Menhir *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
| ParserFront.Point point ->
|
2020-01-08 16:39:52 +01:00
|
|
|
let error =
|
|
|
|
ParserFront.format_error ~offsets:IO.options#offsets
|
|
|
|
IO.options#mode point
|
2020-01-14 01:27:35 +01:00
|
|
|
in close_all (); Stdlib.Error error
|
2020-01-04 23:32:50 +01:00
|
|
|
(* Monolithic API of Menhir *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-04 23:32:50 +01:00
|
|
|
| Parser.Error ->
|
2020-01-08 16:39:52 +01:00
|
|
|
let invalid, valid_opt =
|
2020-01-04 23:32:50 +01:00
|
|
|
match get_win () with
|
|
|
|
Lexer.Nil ->
|
2020-01-14 01:27:35 +01:00
|
|
|
assert false (* Safe: There is always at least EOF. *)
|
2020-01-04 23:32:50 +01:00
|
|
|
| Lexer.One invalid -> invalid, None
|
|
|
|
| Lexer.Two (invalid, valid) -> invalid, Some valid in
|
2020-01-08 16:39:52 +01:00
|
|
|
let point = "", valid_opt, invalid in
|
|
|
|
let error =
|
|
|
|
ParserFront.format_error ~offsets:IO.options#offsets
|
2020-01-14 01:27:35 +01:00
|
|
|
IO.options#mode point
|
|
|
|
in close_all (); Stdlib.Error error
|
2020-01-04 23:32:50 +01:00
|
|
|
|
|
|
|
(* I/O errors *)
|
2020-01-08 16:39:52 +01:00
|
|
|
|
2020-01-14 01:27:35 +01:00
|
|
|
| Sys_error error -> Stdlib.Error error
|
2020-01-04 23:32:50 +01:00
|
|
|
|
|
|
|
end
|