From 320e31cb411763ce788195fd1e3459b66e9ad9bf Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Mon, 25 May 2020 20:29:48 +0200 Subject: [PATCH] * [ligo pretty-print foo.mligo] works. * [ligo pretty-print foo.religo] is a transpiler (for now) and needs more work. * Renamed functions *_ast into *_cst. One day, module AST should be renamed CST for clarity and consistency. --- src/bin/cli.ml | 17 ++++++- src/main/compile/helpers.ml | 56 +++++++++++++++++---- src/main/compile/of_source.ml | 7 ++- src/passes/1-parser/cameligo/ParserLog.ml | 2 +- src/passes/1-parser/cameligo/ParserLog.mli | 2 +- src/passes/1-parser/pascaligo/ParserLog.ml | 2 +- src/passes/1-parser/pascaligo/ParserLog.mli | 2 +- src/passes/1-parser/shared/ParserUnit.ml | 4 +- src/passes/1-parser/shared/ParserUnit.mli | 2 +- 9 files changed, 73 insertions(+), 21 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index c7798eeb3..c8da92924 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -159,10 +159,22 @@ let preprocess = let doc = "Subcommand: Preprocess the source file.\nWarning: Intended for development of LIGO and can break at any time." in (Term.ret term, Term.info ~doc cmdname) +let pretty_print = + let f source_file syntax display_format = ( + toplevel ~display_format @@ + let%bind pp = + Compile.Of_source.pretty_print source_file (Syntax_name syntax) in + ok @@ Buffer.contents pp + ) in + let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let cmdname = "pretty-print" in + let doc = "Subcommand: Pretty-print the source file." + in (Term.ret term, Term.info ~doc cmdname) + let print_cst = let f source_file syntax display_format = ( toplevel ~display_format @@ - let%bind pp = Compile.Of_source.pretty_print source_file (Syntax_name syntax) in + let%bind pp = Compile.Of_source.pretty_print_cst source_file (Syntax_name syntax) in ok @@ Format.asprintf "%s \n" (Buffer.contents pp) ) in @@ -489,5 +501,6 @@ let run ?argv () = print_ast_typed ; print_mini_c ; list_declarations ; - preprocess + preprocess; + pretty_print ] diff --git a/src/main/compile/helpers.ml b/src/main/compile/helpers.ml index b6809a20a..8e35e2887 100644 --- a/src/main/compile/helpers.ml +++ b/src/main/compile/helpers.ml @@ -129,7 +129,7 @@ let parsify_string syntax source = let%bind applied = Self_ast_imperative.all_program parsified in ok applied -let pretty_print_pascaligo source = +let pretty_print_pascaligo_cst source = let%bind ast = Parser.Pascaligo.parse_file source in let buffer = Buffer.create 59 in let state = @@ -137,10 +137,10 @@ let pretty_print_pascaligo source = ~offsets:true ~mode:`Byte ~buffer in - Parser_pascaligo.ParserLog.pp_ast state ast; + Parser_pascaligo.ParserLog.pp_cst state ast; ok buffer -let pretty_print_cameligo source = +let pretty_print_cameligo_cst source = let%bind ast = Parser.Cameligo.parse_file source in let buffer = Buffer.create 59 in let state = (* TODO: Should flow from the CLI *) @@ -148,10 +148,10 @@ let pretty_print_cameligo source = ~offsets:true ~mode:`Point ~buffer in - Parser_cameligo.ParserLog.pp_ast state ast; + Parser_cameligo.ParserLog.pp_cst state ast; ok buffer -let pretty_print_reasonligo source = +let pretty_print_reasonligo_cst source = let%bind ast = Parser.Reasonligo.parse_file source in let buffer = Buffer.create 59 in let state = (* TODO: Should flow from the CLI *) @@ -159,16 +159,16 @@ let pretty_print_reasonligo source = ~offsets:true ~mode:`Point ~buffer in - Parser_cameligo.ParserLog.pp_ast state ast; + Parser_cameligo.ParserLog.pp_cst state ast; ok buffer -let pretty_print syntax source = +let pretty_print_cst syntax source = let%bind v_syntax = syntax_to_variant syntax (Some source) in match v_syntax with - PascaLIGO -> pretty_print_pascaligo source - | CameLIGO -> pretty_print_cameligo source - | ReasonLIGO -> pretty_print_reasonligo source + PascaLIGO -> pretty_print_pascaligo_cst source + | CameLIGO -> pretty_print_cameligo_cst source + | ReasonLIGO -> pretty_print_reasonligo_cst source let preprocess_pascaligo = Parser.Pascaligo.preprocess @@ -183,3 +183,39 @@ let preprocess syntax source = PascaLIGO -> preprocess_pascaligo source | CameLIGO -> preprocess_cameligo source | ReasonLIGO -> preprocess_reasonligo source + +let pretty_print_pascaligo source = + let%bind ast = Parser.Pascaligo.parse_file source in + let () = ignore ast in (* TODO *) + let buffer = Buffer.create 131 + in Trace.ok buffer + +let pretty_print_cameligo source = + let%bind ast = Parser.Cameligo.parse_file source in + let doc = Parser_cameligo.Pretty.make ast in + let buffer = Buffer.create 131 in + let width = + match Terminal_size.get_columns () with + None -> 60 + | Some c -> c in + let () = PPrint.ToBuffer.pretty 1.0 width buffer doc + in Trace.ok buffer + +let pretty_print_reasonligo source = + let%bind ast = Parser.Reasonligo.parse_file source in + let doc = Parser_cameligo.Pretty.make ast in (* TODO *) + let buffer = Buffer.create 131 in + let width = + match Terminal_size.get_columns () with + None -> 60 + | Some c -> c in + let () = PPrint.ToBuffer.pretty 1.0 width buffer doc + in Trace.ok buffer + +let pretty_print syntax source = + let%bind v_syntax = + syntax_to_variant syntax (Some source) in + match v_syntax with + PascaLIGO -> pretty_print_pascaligo source + | CameLIGO -> pretty_print_cameligo source + | ReasonLIGO -> pretty_print_reasonligo source diff --git a/src/main/compile/of_source.ml b/src/main/compile/of_source.ml index 75cb9f32c..f0611d4ba 100644 --- a/src/main/compile/of_source.ml +++ b/src/main/compile/of_source.ml @@ -19,8 +19,11 @@ let compile_contract_input : string -> string -> v_syntax -> Ast_imperative.expr let%bind (storage,parameter) = bind_map_pair (compile_expression syntax) (storage,parameter) in ok @@ Ast_imperative.e_pair storage parameter -let pretty_print source_filename syntax = - Helpers.pretty_print syntax source_filename +let pretty_print_cst source_filename syntax = + Helpers.pretty_print_cst syntax source_filename let preprocess source_filename syntax = Helpers.preprocess syntax source_filename + +let pretty_print source_filename syntax = + Helpers.pretty_print syntax source_filename diff --git a/src/passes/1-parser/cameligo/ParserLog.ml b/src/passes/1-parser/cameligo/ParserLog.ml index b51c0782d..0e4c7621f 100644 --- a/src/passes/1-parser/cameligo/ParserLog.ml +++ b/src/passes/1-parser/cameligo/ParserLog.ml @@ -628,7 +628,7 @@ let pp_verbatim state {value=name; region} = let pp_loc_node state name region = pp_ident state {value=name; region} -let rec pp_ast state {decl; _} = +let rec pp_cst state {decl; _} = let apply len rank = pp_declaration (state#pad len rank) in let decls = Utils.nseq_to_list decl in diff --git a/src/passes/1-parser/cameligo/ParserLog.mli b/src/passes/1-parser/cameligo/ParserLog.mli index d16252478..800ea4443 100644 --- a/src/passes/1-parser/cameligo/ParserLog.mli +++ b/src/passes/1-parser/cameligo/ParserLog.mli @@ -27,5 +27,5 @@ val expr_to_string : (** {1 Pretty-printing of AST nodes} *) -val pp_ast : state -> AST.t -> unit +val pp_cst : state -> AST.t -> unit val pp_expr : state -> AST.expr -> unit diff --git a/src/passes/1-parser/pascaligo/ParserLog.ml b/src/passes/1-parser/pascaligo/ParserLog.ml index c22f5379f..eb694f48b 100644 --- a/src/passes/1-parser/pascaligo/ParserLog.ml +++ b/src/passes/1-parser/pascaligo/ParserLog.ml @@ -855,7 +855,7 @@ let pp_verbatim state {value=name; region} = let pp_loc_node state name region = pp_ident state {value=name; region} -let rec pp_ast state {decl; _} = +let rec pp_cst state {decl; _} = let apply len rank = pp_declaration (state#pad len rank) in let decls = Utils.nseq_to_list decl in diff --git a/src/passes/1-parser/pascaligo/ParserLog.mli b/src/passes/1-parser/pascaligo/ParserLog.mli index 955c1590b..7ae739571 100644 --- a/src/passes/1-parser/pascaligo/ParserLog.mli +++ b/src/passes/1-parser/pascaligo/ParserLog.mli @@ -33,5 +33,5 @@ val instruction_to_string : (** {1 Pretty-printing of AST nodes} *) -val pp_ast : state -> AST.t -> unit +val pp_cst : state -> AST.t -> unit val pp_expr : state -> AST.expr -> unit diff --git a/src/passes/1-parser/shared/ParserUnit.ml b/src/passes/1-parser/shared/ParserUnit.ml index 0e7f4eb88..8c15c1db2 100644 --- a/src/passes/1-parser/shared/ParserUnit.ml +++ b/src/passes/1-parser/shared/ParserUnit.ml @@ -32,7 +32,7 @@ module type Printer = val mk_state : offsets:bool -> mode:[`Point|`Byte] -> buffer:Buffer.t -> state - val pp_ast : state -> ast -> unit + val pp_cst : state -> ast -> unit val pp_expr : state -> expr -> unit val print_tokens : state -> ast -> unit val print_expr : state -> expr -> unit @@ -146,7 +146,7 @@ module Make (Lexer: Lexer.S) if SSet.mem "ast" SubIO.options#verbose then begin Buffer.clear output; - ParserLog.pp_ast state ast; + ParserLog.pp_cst state ast; Buffer.output_buffer stdout output end in flush_all (); close (); Ok ast diff --git a/src/passes/1-parser/shared/ParserUnit.mli b/src/passes/1-parser/shared/ParserUnit.mli index a2199ec4e..fe3b90dc1 100644 --- a/src/passes/1-parser/shared/ParserUnit.mli +++ b/src/passes/1-parser/shared/ParserUnit.mli @@ -36,7 +36,7 @@ module type Printer = val mk_state : offsets:bool -> mode:[`Point|`Byte] -> buffer:Buffer.t -> state - val pp_ast : state -> ast -> unit + val pp_cst : state -> ast -> unit val pp_expr : state -> expr -> unit val print_tokens : state -> ast -> unit val print_expr : state -> expr -> unit