Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
cpp: added a -dump_cpp_full for simonhf@...
Browse files Browse the repository at this point in the history
  • Loading branch information
pad committed Aug 23, 2011
1 parent c01b9ef commit bc94dc7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
22 changes: 10 additions & 12 deletions lang_cpp/parsing/export_ast_cpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module M = Meta_ast_generic

(*
* It can be useful for people who don't like OCaml to still benefit
* from pfff parsing by having at least a JSON representation
* from pfff parsing by having at least a JSON-like representation
* of the Ast, hence this file.
*)

Expand All @@ -38,6 +38,9 @@ let for_json = {
M.token_info = false;
}

let no_info =
for_json

let string_json_of_program x =
x |> Meta_ast_cpp.vof_program for_json
|> Ocaml.json_of_v |> Json_out.string_of_json
Expand All @@ -46,7 +49,7 @@ let string_json_of_program x =
(* ML Patterns *)
(*****************************************************************************)

let string_of_v v =
let string_of_v precision v =
let cnt = ref 0 in

(* transformation to not have the parse info or type info in the output *)
Expand All @@ -56,7 +59,9 @@ let string_of_v v =
incr cnt;
(match () with
| _ when xs +> List.exists (function ("token", _) -> true | _ -> false)->
Ocaml.VVar ("i", Int64.of_int !cnt)
if not precision.M.token_info
then Ocaml.VVar ("i", Int64.of_int !cnt)
else x
| _ when xs +> List.exists (function ("t", _) -> true | _ -> false)->
Ocaml.VVar ("t", Int64.of_int !cnt)
| _ when xs +> List.exists (function ("tvar", _) -> true | _ -> false)->
Expand All @@ -73,13 +78,6 @@ let string_of_v v =
s


let ml_pattern_string_of_program ast =

let precision = { Meta_ast_generic.
full_info = false;
token_info = false;
type_info = false;
}
in
Meta_ast_cpp.vof_program precision ast +> string_of_v
let ml_pattern_string_of_program ?(precision=no_info) ast =
Meta_ast_cpp.vof_program precision ast +> string_of_v precision

4 changes: 3 additions & 1 deletion lang_cpp/parsing/export_ast_cpp.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

val ml_pattern_string_of_program: Ast_cpp.program -> string
val ml_pattern_string_of_program:
?precision:Meta_ast_generic.precision ->
Ast_cpp.program -> string

val string_json_of_program: Ast_cpp.program -> string

24 changes: 24 additions & 0 deletions lang_cpp/parsing/test_parsing_cpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Common
open Ast_cpp
module Ast = Ast_cpp
module Flag = Flag_parsing_cpp
module TH = Token_helpers_cpp

module Stat = Statistics_parsing

Expand Down Expand Up @@ -71,6 +72,27 @@ let test_dump_cpp file =
let s = Export_ast_cpp.ml_pattern_string_of_program ast in
pr s

let test_dump_cpp_full file =
Parse_cpp.init_defs !Flag.macros_h;
let ast = Parse_cpp.parse_program file in
let toks = Parse_cpp.tokens file in
let precision = { Meta_ast_generic.
full_info = true; type_info = false; token_info = true;
}
in
let s = Export_ast_cpp.ml_pattern_string_of_program ~precision ast in
pr s;
toks +> List.iter (fun tok ->
match tok with
| Parser_cpp.TComment (ii) ->
let v = Parse_info.vof_info ii in
let s = Ocaml.string_of_v v in
pr s
| _ -> ()
);
()


let test_view_cpp file =
let toks = Parse_cpp.tokens file
+> Common.exclude Token_helpers_cpp.is_comment in
Expand Down Expand Up @@ -101,6 +123,8 @@ let actions () = [
Common.mk_action_1_arg test_dump_cpp;
"-view_cpp", " <file>",
Common.mk_action_1_arg test_view_cpp;
"-dump_cpp_full", " <file>",
Common.mk_action_1_arg test_dump_cpp_full;

"-parse_c", " <file or dir>",
Common.mk_action_n_arg (fun xs -> test_parse_cpp ~c:true xs);
Expand Down

0 comments on commit bc94dc7

Please sign in to comment.