3ed303f60d
like the absence of an input filename. (This simplifies all the clients codes.) Fixed the dune file for the preprocessor. Fixed the build of PreprocMain.exe and PreprocMain.byte. Restricted preprocessing errors [Preproc.Newline_in_string] and [Preproc.Open_string] to the argument of the #include directive (instead of general strings: this is for the LIGO lexer to report the error). I removed the error [Preproc.Open_comment] as this is for the LIGO lexer to report. The preprocessor scanner [Preproc.lex] does not take a parameter [is_file:bool] now: the source file (if any) is determined from the lexing buffer. Accordingly, the field [is_file] of the state of the preprocessing lexer has been removed: the lexing buffer becomes now the reference for the input source (bug fix and interface improvement). Fixed the comments of the test contract pledge.religo. I removed the data constructor [Lexer.Stdin], as redundant with [Lexer.Channel].
36 lines
1.1 KiB
OCaml
36 lines
1.1 KiB
OCaml
(* Standalone preprocessor for PascaLIGO *)
|
|
|
|
module Region = Simple_utils.Region
|
|
module Preproc = Preprocessor.Preproc
|
|
module EvalOpt = Preprocessor.EvalOpt
|
|
|
|
let highlight msg = Printf.eprintf "\027[31m%s\027[0m\n%!" msg
|
|
|
|
let options = EvalOpt.(read ~lang:`PascaLIGO ~ext:".ligo")
|
|
|
|
let preproc cin =
|
|
let buffer = Lexing.from_channel cin in
|
|
let open Lexing in
|
|
let () =
|
|
match options#input with
|
|
None -> ()
|
|
| 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
|
|
None -> preproc stdin
|
|
| Some file_path ->
|
|
try open_in file_path |> preproc with
|
|
Sys_error msg -> highlight msg
|