Streamlined LexerMain and ParserMain for ReasonLIGO.

This commit is contained in:
Christian Rinderknecht 2019-12-13 17:28:15 +01:00
parent 2904a40373
commit 6692643cc5
5 changed files with 66 additions and 67 deletions

View File

@ -1 +0,0 @@
ocamlc: -w -42

View File

@ -1 +0,0 @@
ocamlc: -w -58

View File

@ -1,2 +0,0 @@
module Region = Region
module Pos = Pos

View File

@ -1,44 +1,40 @@
(* Driver for the lexer of ReasonLIGO *) (** Driver for the LIGO lexer *)
(* Error printing and exception tracing *) let extension = ".religo"
let options = EvalOpt.read "ReasonLIGO" extension
(** Error printing and exception tracing
*)
let () = Printexc.record_backtrace true let () = Printexc.record_backtrace true
(* Running the lexer on the source *)
let options = EvalOpt.read "ReasonLIGO" ".religo"
open EvalOpt
let external_ text = let external_ text =
Utils.highlight (Printf.sprintf "External error: %s" text); exit 1;; Utils.highlight (Printf.sprintf "External error: %s" text); exit 1;;
(* Path for CPP inclusions (#include) *) (** {1 Preprocessing the input source and opening the input channels} *)
(** Path for CPP inclusions (#include)
*)
let lib_path = let lib_path =
match options.libs with match options#libs with
[] -> "" [] -> ""
| libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path | libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path
in List.fold_right mk_I libs "" in List.fold_right mk_I libs ""
(* Preprocessing the input source and opening the input channels *)
let prefix = let prefix =
match options.input with match options#input with
None | Some "-" -> "temp" None | Some "-" -> "temp"
| Some file -> Filename.(file |> basename |> remove_extension) | Some file -> Filename.(file |> basename |> remove_extension)
let suffix = ".pp.religo" let suffix = ".pp" ^ extension
let pp_input = let pp_input =
if Utils.String.Set.mem "cpp" options.verbose if Utils.String.Set.mem "cpp" options#verbose
then prefix ^ suffix then prefix ^ suffix
else let pp_input, pp_out = Filename.open_temp_file prefix suffix else let pp_input, pp_out = Filename.open_temp_file prefix suffix
in close_out pp_out; pp_input in close_out pp_out; pp_input
let cpp_cmd = let cpp_cmd =
match options#input with
match options.input with
None | Some "-" -> None | Some "-" ->
Printf.sprintf "cpp -traditional-cpp%s - > %s" Printf.sprintf "cpp -traditional-cpp%s - > %s"
lib_path pp_input lib_path pp_input
@ -47,16 +43,14 @@ let cpp_cmd =
lib_path file pp_input lib_path file pp_input
let () = let () =
if Utils.String.Set.mem "cpp" options.verbose if Utils.String.Set.mem "cpp" options#verbose
then Printf.eprintf "%s\n%!" cpp_cmd; then Printf.eprintf "%s\n%!" cpp_cmd;
if Sys.command cpp_cmd <> 0 then if Sys.command cpp_cmd <> 0 then
external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd) external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd)
(* Running the lexer on the input file *) (** {1 Running the lexer on the input file} *)
module Lexer = Lexer.Make (LexToken) module Log = LexerLog.Make (Lexer.Make (LexToken))
module Log = LexerLog.Make (Lexer) let () = Log.trace ~offsets:options#offsets
options#mode (Some pp_input) options#cmd
let () = Log.trace ~offsets:options.offsets
options.mode (Some pp_input) options.cmd

View File

@ -1,27 +1,24 @@
(* Driver for the parser of ReasonLIGO *) (** Driver for the LIGO parser *)
(* Error printing and exception tracing *) let extension = ".religo"
let options = EvalOpt.read "ReasonLIGO" extension
(** Error printing and exception tracing
*)
let () = Printexc.record_backtrace true let () = Printexc.record_backtrace true
(* Reading the command-line options *) (** Auxiliary functions
*)
let options = EvalOpt.read "ReasonLIGO" ".religo"
open EvalOpt
(* Auxiliary functions *)
let sprintf = Printf.sprintf let sprintf = Printf.sprintf
(* Extracting the input file *) (** Extracting the input file
*)
let file = let file =
match options.input with match options#input with
None | Some "-" -> false None | Some "-" -> false
| Some _ -> true | Some _ -> true
(* Error printing and exception tracing *) (** {1 Error printing and exception tracing} *)
let () = Printexc.record_backtrace true let () = Printexc.record_backtrace true
@ -35,35 +32,35 @@ let error_to_string = function
| _ -> assert false | _ -> assert false
let print_error ?(offsets=true) mode Region.{region; value} ~file = let print_error ?(offsets=true) mode Region.{region; value} ~file =
let msg = error_to_string value in let msg = error_to_string value in
let reg = region#to_string ~file ~offsets mode in let reg = region#to_string ~file ~offsets mode in
Utils.highlight (sprintf "Parse error %s:\n%s%!" reg msg) Utils.highlight (sprintf "Parse error %s:\n%s%!" reg msg)
(* Path for CPP inclusions (#include) *) (** {1 Preprocessing the input source and opening the input channels} *)
(** Path for CPP inclusions (#include)
*)
let lib_path = let lib_path =
match options.libs with match options#libs with
[] -> "" [] -> ""
| libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path | libs -> let mk_I dir path = Printf.sprintf " -I %s%s" dir path
in List.fold_right mk_I libs "" in List.fold_right mk_I libs ""
(* Preprocessing the input source and opening the input channels *)
let prefix = let prefix =
match options.input with match options#input with
None | Some "-" -> "temp" None | Some "-" -> "temp"
| Some file -> Filename.(file |> basename |> remove_extension) | Some file -> Filename.(file |> basename |> remove_extension)
let suffix = ".pp.religo" let suffix = ".pp" ^ extension
let pp_input = let pp_input =
if Utils.String.Set.mem "cpp" options.verbose if Utils.String.Set.mem "cpp" options#verbose
then prefix ^ suffix then prefix ^ suffix
else let pp_input, pp_out = Filename.open_temp_file prefix suffix else let pp_input, pp_out = Filename.open_temp_file prefix suffix
in close_out pp_out; pp_input in close_out pp_out; pp_input
let cpp_cmd = let cpp_cmd =
match options.input with match options#input with
None | Some "-" -> None | Some "-" ->
Printf.sprintf "cpp -traditional-cpp%s - > %s" Printf.sprintf "cpp -traditional-cpp%s - > %s"
lib_path pp_input lib_path pp_input
@ -72,12 +69,12 @@ let cpp_cmd =
lib_path file pp_input lib_path file pp_input
let () = let () =
if Utils.String.Set.mem "cpp" options.verbose if Utils.String.Set.mem "cpp" options#verbose
then Printf.eprintf "%s\n%!" cpp_cmd; then Printf.eprintf "%s\n%!" cpp_cmd;
if Sys.command cpp_cmd <> 0 then if Sys.command cpp_cmd <> 0 then
external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd) external_ (Printf.sprintf "the command \"%s\" failed." cpp_cmd)
(* Instanciating the lexer *) (** {1 Instanciating the lexer} *)
module Lexer = Lexer.Make (LexToken) module Lexer = Lexer.Make (LexToken)
@ -88,37 +85,49 @@ let Lexer.{read; buffer; get_pos; get_last; close} =
and cout = stdout and cout = stdout
let log = Log.output_token ~offsets:options.offsets let log = Log.output_token ~offsets:options#offsets
options.mode options.cmd cout options#mode options#cmd cout
and close_all () = close (); close_out cout and close_all () = close (); close_out cout
(* Tokeniser *) (** {1 Tokeniser} *)
let tokeniser = read ~log let tokeniser = read ~log
(* Main *) (** {1 Main} *)
let () = let () =
try try
let ast = Parser.contract tokeniser buffer in let ast = Parser.contract tokeniser buffer in
if Utils.String.Set.mem "ast" options.verbose if Utils.String.Set.mem "ast" options#verbose
then let buffer = Buffer.create 131 in then let buffer = Buffer.create 131 in
let state = ParserLog.mk_state
~offsets:options#offsets
~mode:options#mode
~buffer in
begin begin
Parser_cameligo.ParserLog.offsets := options.offsets; ParserLog.pp_ast state ast;
Parser_cameligo.ParserLog.mode := options.mode; Buffer.output_buffer stdout buffer
Parser_cameligo.ParserLog.print_tokens buffer ast; end
else if Utils.String.Set.mem "ast-tokens" options#verbose
then let buffer = Buffer.create 131 in
let state = ParserLog.mk_state
~offsets:options#offsets
~mode:options#mode
~buffer in
begin
ParserLog.print_tokens state ast;
Buffer.output_buffer stdout buffer Buffer.output_buffer stdout buffer
end end
with with
Lexer.Error err -> Lexer.Error err ->
close_all (); close_all ();
Lexer.print_error ~offsets:options.offsets Lexer.print_error ~offsets:options#offsets
options.mode err ~file options#mode err ~file
| Parser.Error -> | Parser.Error ->
let region = get_last () in let region = get_last () in
let error = Region.{region; value=ParseError} in let error = Region.{region; value=ParseError} in
let () = close_all () in let () = close_all () in
print_error ~offsets:options.offsets print_error ~offsets:options#offsets
options.mode error ~file options#mode error ~file
| Sys_error msg -> Utils.highlight msg | Sys_error msg -> Utils.highlight msg