2019-12-13 20:28:15 +04:00
|
|
|
(** Driver for the LIGO parser *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
let extension = ".religo"
|
|
|
|
let options = EvalOpt.read "ReasonLIGO" extension
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** Error printing and exception tracing
|
|
|
|
*)
|
2019-12-10 17:47:31 +04:00
|
|
|
let () = Printexc.record_backtrace true
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** Auxiliary functions
|
|
|
|
*)
|
2019-12-10 17:47:31 +04:00
|
|
|
let sprintf = Printf.sprintf
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** Extracting the input file
|
|
|
|
*)
|
2019-12-10 17:47:31 +04:00
|
|
|
let file =
|
2019-12-13 20:28:15 +04:00
|
|
|
match options#input with
|
2019-12-10 17:47:31 +04:00
|
|
|
None | Some "-" -> false
|
|
|
|
| Some _ -> true
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** {1 Error printing and exception tracing} *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let () = Printexc.record_backtrace true
|
|
|
|
|
|
|
|
let external_ text =
|
|
|
|
Utils.highlight (Printf.sprintf "External error: %s" text); exit 1;;
|
|
|
|
|
2019-12-17 17:56:16 +04:00
|
|
|
type error = SyntaxError
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let error_to_string = function
|
2019-12-17 17:56:16 +04:00
|
|
|
SyntaxError -> "Syntax error.\n"
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let print_error ?(offsets=true) mode Region.{region; value} ~file =
|
2019-12-17 17:56:16 +04:00
|
|
|
let msg = error_to_string value
|
|
|
|
and reg = region#to_string ~file ~offsets mode in
|
2019-12-10 17:47:31 +04:00
|
|
|
Utils.highlight (sprintf "Parse error %s:\n%s%!" reg msg)
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** {1 Preprocessing the input source and opening the input channels} *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** Path for CPP inclusions (#include)
|
|
|
|
*)
|
2019-12-10 17:47:31 +04:00
|
|
|
let lib_path =
|
2019-12-13 20:28:15 +04:00
|
|
|
match options#libs with
|
2019-12-10 17:47:31 +04:00
|
|
|
[] -> ""
|
|
|
|
| libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path
|
2019-12-13 20:28:15 +04:00
|
|
|
in List.fold_right mk_I libs ""
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let prefix =
|
2019-12-13 20:28:15 +04:00
|
|
|
match options#input with
|
2019-12-10 17:47:31 +04:00
|
|
|
None | Some "-" -> "temp"
|
|
|
|
| Some file -> Filename.(file |> basename |> remove_extension)
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
let suffix = ".pp" ^ extension
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let pp_input =
|
2019-12-13 20:28:15 +04:00
|
|
|
if Utils.String.Set.mem "cpp" options#verbose
|
2019-12-10 17:47:31 +04:00
|
|
|
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 =
|
2019-12-13 20:28:15 +04:00
|
|
|
match options#input with
|
2019-12-10 17:47:31 +04:00
|
|
|
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 () =
|
2019-12-13 20:28:15 +04:00
|
|
|
if Utils.String.Set.mem "cpp" options#verbose
|
2019-12-10 17:47:31 +04:00
|
|
|
then Printf.eprintf "%s\n%!" cpp_cmd;
|
|
|
|
if Sys.command cpp_cmd <> 0 then
|
|
|
|
external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd)
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** {1 Instanciating the lexer} *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
module Lexer = Lexer.Make (LexToken)
|
|
|
|
module Log = LexerLog.Make (Lexer)
|
2019-12-17 20:03:43 +04:00
|
|
|
module ParserFront = ParserAPI.Make (Lexer) (Parser)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2019-12-17 20:03:43 +04:00
|
|
|
let lexer_inst = Lexer.open_token_stream (Some pp_input)
|
|
|
|
let Lexer.{read; buffer; get_pos; get_last; close} = lexer_inst
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
and cout = stdout
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
let log = Log.output_token ~offsets:options#offsets
|
|
|
|
options#mode options#cmd cout
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
and close_all () = close (); close_out cout
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** {1 Tokeniser} *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let tokeniser = read ~log
|
|
|
|
|
2019-12-13 20:28:15 +04:00
|
|
|
(** {1 Main} *)
|
2019-12-10 17:47:31 +04:00
|
|
|
|
|
|
|
let () =
|
|
|
|
try
|
2019-12-17 20:03:43 +04:00
|
|
|
(* The incremental API *)
|
|
|
|
let ast = ParserFront.incr_contract lexer_inst in
|
|
|
|
(* The monolithic API *)
|
|
|
|
(* let ast = ParserFront.mono_contract tokeniser buffer in *)
|
2019-12-13 20:28:15 +04:00
|
|
|
if Utils.String.Set.mem "ast" options#verbose
|
|
|
|
then let buffer = Buffer.create 131 in
|
|
|
|
let state = ParserLog.mk_state
|
|
|
|
~offsets:options#offsets
|
|
|
|
~mode:options#mode
|
|
|
|
~buffer in
|
|
|
|
begin
|
|
|
|
ParserLog.pp_ast state ast;
|
|
|
|
Buffer.output_buffer stdout buffer
|
|
|
|
end
|
|
|
|
else if Utils.String.Set.mem "ast-tokens" options#verbose
|
2019-12-10 17:47:31 +04:00
|
|
|
then let buffer = Buffer.create 131 in
|
2019-12-13 20:28:15 +04:00
|
|
|
let state = ParserLog.mk_state
|
|
|
|
~offsets:options#offsets
|
|
|
|
~mode:options#mode
|
|
|
|
~buffer in
|
2019-12-10 17:47:31 +04:00
|
|
|
begin
|
2019-12-13 20:28:15 +04:00
|
|
|
ParserLog.print_tokens state ast;
|
2019-12-10 17:47:31 +04:00
|
|
|
Buffer.output_buffer stdout buffer
|
|
|
|
end
|
|
|
|
with
|
|
|
|
Lexer.Error err ->
|
|
|
|
close_all ();
|
2019-12-13 20:28:15 +04:00
|
|
|
Lexer.print_error ~offsets:options#offsets
|
|
|
|
options#mode err ~file
|
2019-12-10 17:47:31 +04:00
|
|
|
| Parser.Error ->
|
|
|
|
let region = get_last () in
|
2019-12-17 17:56:16 +04:00
|
|
|
let error = Region.{region; value=SyntaxError} in
|
2019-12-10 17:47:31 +04:00
|
|
|
let () = close_all () in
|
2019-12-13 20:28:15 +04:00
|
|
|
print_error ~offsets:options#offsets
|
|
|
|
options#mode error ~file
|
2019-12-10 17:47:31 +04:00
|
|
|
| Sys_error msg -> Utils.highlight msg
|