2020-03-31 21:44:10 +04:00
|
|
|
(* Standalone preprocessor for PascaLIGO *)
|
|
|
|
|
2020-04-09 18:18:26 +04:00
|
|
|
module Region = Simple_utils.Region
|
|
|
|
module Preproc = Preprocessor.Preproc
|
|
|
|
module EvalOpt = Preprocessor.EvalOpt
|
2020-03-31 21:44:10 +04:00
|
|
|
|
2020-04-24 22:54:13 +04:00
|
|
|
let highlight msg = Printf.eprintf "\027[31m%s\027[0m%!" msg
|
2020-03-31 21:44:10 +04:00
|
|
|
|
2020-04-24 22:54:13 +04:00
|
|
|
let options =
|
|
|
|
let open EvalOpt in
|
|
|
|
let block = mk_block ~opening:"(*" ~closing:"*)"
|
|
|
|
in read ~block ~line:"//" ".ligo"
|
2020-03-31 21:44:10 +04:00
|
|
|
|
|
|
|
let preproc cin =
|
|
|
|
let buffer = Lexing.from_channel cin in
|
|
|
|
let open Lexing in
|
|
|
|
let () =
|
|
|
|
match options#input with
|
2020-04-09 18:18:26 +04:00
|
|
|
None -> ()
|
2020-03-31 21:44:10 +04:00
|
|
|
| Some pos_fname ->
|
|
|
|
buffer.lex_curr_p <- {buffer.lex_curr_p with pos_fname} in
|
|
|
|
match Preproc.lex options buffer with
|
|
|
|
Stdlib.Ok pp_buffer -> print_string (Buffer.contents pp_buffer)
|
|
|
|
| Stdlib.Error (pp_buffer, err) ->
|
|
|
|
let formatted =
|
|
|
|
Preproc.format ~offsets:options#offsets ~file:true err in
|
|
|
|
begin
|
|
|
|
if EvalOpt.SSet.mem "preproc" options#verbose then
|
|
|
|
Printf.printf "%s\n%!" (Buffer.contents pp_buffer);
|
|
|
|
highlight formatted.Region.value
|
|
|
|
end
|
|
|
|
|
|
|
|
let () =
|
|
|
|
match options#input with
|
2020-04-09 18:18:26 +04:00
|
|
|
None -> preproc stdin
|
2020-03-31 21:44:10 +04:00
|
|
|
| Some file_path ->
|
|
|
|
try open_in file_path |> preproc with
|
|
|
|
Sys_error msg -> highlight msg
|