···308308clean::
309309 rm -f primreq
310310311311-# Dump the Parsetree representation of OCaml code, in OCaml syntax
312312-313313-OCAMLAST=dump_ast.cmo
314314-315315-ocamlast: $(OCAMLAST)
316316- $(CAMLC) $(LINKFLAGS) -o ocamlast ../compilerlibs/ocamlcommon.cma ../compilerlibs/ocamlbytecomp.cma ../compilerlibs/ocamltoplevel.cma $(OCAMLAST)
317317-318318-clean::
319319- rm -rf ocamlast
320320-321311# Common stuff
322312323313.SUFFIXES:
-127
tools/dump_ast.ml
···11-(***********************************************************************)
22-(* *)
33-(* OCaml *)
44-(* *)
55-(* Alain Frisch, LexiFi *)
66-(* *)
77-(* Copyright 2012 Institut National de Recherche en Informatique et *)
88-(* en Automatique. All rights reserved. This file is distributed *)
99-(* under the terms of the Q Public License version 1.0. *)
1010-(* *)
1111-(***********************************************************************)
1212-1313-let autoopen_modules = ["Parsetree"; "Asttypes"; "Longident"; "Location"; "Lexing"]
1414-let autoopen = ref true
1515-1616-let typ ty =
1717- let ty = Parse.core_type (Lexing.from_string ty) in
1818- let ty = Typetexp.transl_simple_type Env.initial false ty in
1919- ty.Typedtree.ctyp_type
2020-2121-let show_value ty v =
2222- let env = Env.initial in
2323- let env = if !autoopen then List.fold_right Env.open_pers_signature autoopen_modules env else env in
2424- Format.printf "%a@." (Toploop.print_value env (Obj.repr v)) (typ ty)
2525-2626-let dump_file fn =
2727- let ic = open_in fn in
2828- let buf = Lexing.from_channel ic in
2929- if Filename.check_suffix fn ".mli"
3030- then show_value "Parsetree.signature" (Parse.interface buf)
3131- else show_value "Parsetree.structure" (Parse.implementation buf);
3232- close_in ic
3333-3434-let dump_expr s =
3535- show_value "Parsetree.expression" (Parse.expression (Lexing.from_string s))
3636-3737-let dump_type s =
3838- show_value "Parsetree.core_type" (Parse.core_type (Lexing.from_string s))
3939-4040-let dump_pattern s =
4141- show_value "Parsetree.pattern" (Parse.pattern (Lexing.from_string s))
4242-4343-(* Filtering of output *)
4444-4545-module Filter = struct
4646- open Outcometree
4747-4848- let hidelocs = ref true
4949- let hideattrs = ref true
5050-5151- let map_oval f = function
5252- | Oval_array l -> Oval_array (List.map f l)
5353- | Oval_constr (i, l) -> Oval_constr (i, List.map f l)
5454- | Oval_list l -> Oval_list (List.map f l)
5555- | Oval_record l -> Oval_record (List.map (fun (s, x) -> (s, f x)) l)
5656- | Oval_tuple l -> Oval_tuple (List.map f l)
5757- | Oval_variant (s, Some x) -> Oval_variant (s, Some (f x))
5858- | x -> x
5959-6060- let ends_with s l =
6161- let ll = String.length l and ls = String.length s in
6262- ll >= ls && String.sub l (ll - ls) ls = s
6363-6464- let filter_field = function
6565- | (Oide_ident l, Oval_list [])
6666- when !hideattrs && ends_with "_attributes" l -> false
6767- | (Oide_ident l, _)
6868- when !hidelocs && (ends_with "_loc" l || l = "loc") -> false
6969- | _ -> true
7070-7171- let rec filter_val x =
7272- match map_oval filter_val x with
7373- | Oval_record l -> Oval_record (List.filter filter_field l)
7474- | x -> x
7575-7676- let () =
7777- let old = !Oprint.out_value in
7878- Oprint.out_value := (fun ppf v -> old ppf (filter_val v))
7979-end
8080-8181-(* Command-line parsing *)
8282-8383-let args =
8484- let open Arg in
8585- [
8686- "-noopen", Clear autoopen,
8787- " Don't assume that default modules are opened";
8888-8989- "-locs", Clear Filter.hidelocs,
9090- " Keep locations";
9191-9292- "-emptyattrs", Clear Filter.hideattrs,
9393- " Keep empty attributes";
9494-9595- "-e", String dump_expr,
9696- "<expr> Dump the AST for <expr>";
9797-9898- "-t", String dump_type,
9999- "<type> Dump the AST for <type>";
100100-101101- "-p", String dump_pattern,
102102- "<pattern> Dump the AST for <pattern>";
103103-104104- "-w", Int Format.set_margin,
105105- "<width> Define the width (in characters) of the output"
106106- ]
107107-108108-let usage = "dump_ast [options] <.ml/.mli source files>\n"
109109-110110-let () =
111111- let dir = Filename.dirname Sys.argv.(0) in
112112- let c0 = Filename.concat dir in
113113- let c1 = Filename.concat Config.standard_library in
114114-115115- if Sys.file_exists (c1 "compiler-libs/parsetree.cmi")
116116- then Config.load_path := [c1 "compiler-libs"; Config.standard_library]
117117- else if Sys.file_exists (c0 "../parsing/parsetree.cmi")
118118- (* Running from a source tree. *)
119119- then Config.load_path := [c0 "../parsing"; c0 "../stdlib"]
120120- else (prerr_endline "Cannot locate parsetree.cmi"; exit 2);
121121-122122- Toploop.initialize_toplevel_env ();
123123- Toploop.max_printer_depth := max_int;
124124- Toploop.max_printer_steps := max_int;
125125-126126- try Arg.parse (Arg.align args) dump_file usage
127127- with exn -> Errors.report_error Format.err_formatter exn