2019-09-27 17:33:25 +04:00
|
|
|
(* Driver for the parser of Ligodity *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
(* Error printing and exception tracing *)
|
|
|
|
|
2019-07-24 17:43:51 +04:00
|
|
|
let () = Printexc.record_backtrace true
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-05-15 17:03:15 +04:00
|
|
|
(* Reading the command-line options *)
|
|
|
|
|
2019-10-07 18:33:34 +04:00
|
|
|
let options = EvalOpt.read "CameLIGO" ".mligo"
|
2019-05-15 17:03:15 +04:00
|
|
|
|
|
|
|
open EvalOpt
|
|
|
|
|
2019-09-27 17:33:25 +04:00
|
|
|
(* Auxiliary functions *)
|
|
|
|
|
|
|
|
let sprintf = Printf.sprintf
|
|
|
|
|
|
|
|
(* Extracting the input file *)
|
|
|
|
|
|
|
|
let file =
|
|
|
|
match options.input with
|
|
|
|
None | Some "-" -> false
|
|
|
|
| Some _ -> true
|
|
|
|
|
|
|
|
(* Error printing and exception tracing *)
|
|
|
|
|
|
|
|
let () = Printexc.record_backtrace true
|
|
|
|
|
|
|
|
let external_ text =
|
|
|
|
Utils.highlight (Printf.sprintf "External error: %s" text); exit 1;;
|
|
|
|
|
|
|
|
type Error.t += ParseError
|
|
|
|
|
|
|
|
let error_to_string = function
|
|
|
|
ParseError -> "Syntax error.\n"
|
|
|
|
| _ -> assert false
|
|
|
|
|
|
|
|
let print_error ?(offsets=true) mode Region.{region; value} ~file =
|
|
|
|
let msg = error_to_string value in
|
|
|
|
let reg = region#to_string ~file ~offsets mode in
|
|
|
|
Utils.highlight (sprintf "Parse error %s:\n%s%!" reg msg)
|
|
|
|
|
|
|
|
(* Path for CPP inclusions (#include) *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
let lib_path =
|
2019-05-15 17:03:15 +04:00
|
|
|
match options.libs with
|
2019-05-12 21:31:22 +04:00
|
|
|
[] -> ""
|
|
|
|
| libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path
|
|
|
|
in List.fold_right mk_I libs ""
|
|
|
|
|
2019-09-27 17:33:25 +04:00
|
|
|
(* Preprocessing the input source and opening the input channels *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-09-27 17:33:25 +04:00
|
|
|
let prefix =
|
2019-05-15 17:03:15 +04:00
|
|
|
match options.input with
|
2019-09-27 17:33:25 +04:00
|
|
|
None | Some "-" -> "temp"
|
|
|
|
| Some file -> Filename.(file |> basename |> remove_extension)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
2019-09-27 17:33:25 +04:00
|
|
|
let suffix = ".pp.mligo"
|
|
|
|
|
|
|
|
let pp_input =
|
|
|
|
if Utils.String.Set.mem "cpp" options.verbose
|
|
|
|
then prefix ^ suffix
|
|
|
|
else let pp_input, pp_out = Filename.open_temp_file prefix suffix
|
|
|
|
in close_out pp_out; pp_input
|
|
|
|
|
|
|
|
let cpp_cmd =
|
|
|
|
match options.input with
|
|
|
|
None | Some "-" ->
|
|
|
|
Printf.sprintf "cpp -traditional-cpp%s - > %s"
|
|
|
|
lib_path pp_input
|
|
|
|
| Some file ->
|
|
|
|
Printf.sprintf "cpp -traditional-cpp%s %s > %s"
|
|
|
|
lib_path file pp_input
|
|
|
|
|
|
|
|
let () =
|
|
|
|
if Utils.String.Set.mem "cpp" options.verbose
|
|
|
|
then Printf.eprintf "%s\n%!" cpp_cmd;
|
|
|
|
if Sys.command cpp_cmd <> 0 then
|
|
|
|
external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd)
|
|
|
|
|
|
|
|
(* Instanciating the lexer *)
|
|
|
|
|
|
|
|
module Lexer = Lexer.Make (LexToken)
|
|
|
|
|
|
|
|
module Log = LexerLog.Make (Lexer)
|
|
|
|
|
|
|
|
let Lexer.{read; buffer; get_pos; get_last; close} =
|
|
|
|
Lexer.open_token_stream (Some pp_input)
|
|
|
|
|
|
|
|
and cout = stdout
|
|
|
|
|
|
|
|
let log = Log.output_token ~offsets:options.offsets
|
|
|
|
options.mode options.cmd cout
|
|
|
|
|
|
|
|
and close_all () = close (); close_out cout
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
(* Tokeniser *)
|
|
|
|
|
2019-09-27 17:33:25 +04:00
|
|
|
let tokeniser = read ~log
|
|
|
|
|
|
|
|
(* Main *)
|
2019-05-12 21:31:22 +04:00
|
|
|
|
|
|
|
let () =
|
|
|
|
try
|
2019-09-27 17:33:25 +04:00
|
|
|
let ast = Parser.contract tokeniser buffer in
|
|
|
|
if Utils.String.Set.mem "ast" options.verbose
|
2019-10-09 18:07:13 +04:00
|
|
|
then let buffer = Buffer.create 131 in
|
|
|
|
begin
|
2019-09-27 17:33:25 +04:00
|
|
|
ParserLog.offsets := options.offsets;
|
|
|
|
ParserLog.mode := options.mode;
|
2019-10-09 18:07:13 +04:00
|
|
|
ParserLog.print_tokens buffer ast;
|
|
|
|
Buffer.output_buffer stdout buffer
|
2019-09-27 17:33:25 +04:00
|
|
|
end
|
2019-05-12 21:31:22 +04:00
|
|
|
with
|
2019-09-27 17:33:25 +04:00
|
|
|
Lexer.Error err ->
|
|
|
|
close_all ();
|
|
|
|
Lexer.print_error ~offsets:options.offsets
|
|
|
|
options.mode err ~file
|
2019-05-12 21:31:22 +04:00
|
|
|
| Parser.Error ->
|
2019-09-27 17:33:25 +04:00
|
|
|
let region = get_last () in
|
|
|
|
let error = Region.{region; value=ParseError} in
|
|
|
|
let () = close_all () in
|
|
|
|
print_error ~offsets:options.offsets
|
|
|
|
options.mode error ~file
|
2019-05-12 21:31:22 +04:00
|
|
|
| Sys_error msg -> Utils.highlight msg
|