Commits

ccorrodi  committed cdeb6e2

refactor debug functions into own module

- add files debug.ml and debug.mli which are used for all debug functions
- ast.ml contains only functions that format strings in the make database
format
- rename pp_* functions to debug_*
- print file section in debug_database

  • Participants
  • Parent commits 9fb08c6
  • Branches develop

Comments (0)

Files changed (9)

+- better naming of print / string functions (especially when distinguishing
+  between debug functions and make database format functions)

File project.odocl

 Ast
+Debug
 Ocamlmake
 Parser
 Lexer
 let s_flavor_append = "+="
 let s_flavor_conditional = "?="
 
-(** {2 String Functions} *)
-(** Denote by how many tabs a line should be indented when using indent. *)
-let d_indent : int ref = ref 0
-
-(** Indent each line in s by i tabs. *)
-let indent_num (i : int) (s : string) : string =
-  S.global_replace (S.regexp "^.*$") (String.make i '\t' ^ "\\0") s
-
-(** Indent all lines by d_indent tabs. *)
-let indent (s : string) : string =
-  indent_num !d_indent s
-
-(** Indent by one tab. *)
-let indent_1 (s : string) : string =
-  indent_num 1 s
-
 (** {2 String / Type Conversion} *)
 (** Convert a string to a command. Remove one leading tab character. *)
 let s_to_command (s : string) : command =
   String.concat " " r.r_targets
 
 (** Convert a dependency to a string. *)
-let dep_to_s (d : dependency) : string =
+let s_of_dep (d : dependency) : string =
   d
 
 (** {2 Initialization} *)
   let file_strings = L.map s_of_rule d.d_files in
   String.concat "\n" file_strings
 
-(** {2 Debug} *)
-
-(** Return a string containing the variable section of the given database,
-    including all information from the variable type. *)
-let pp_ast_vars (d : database) : string =
-  let debug_var (v : variable) : string =
-    let name = v.v_name in
-    let value = v.v_value in
-    let origin =
-      match v.v_origin with
-      | ODefault ->
-          "default"
-      | OEnv ->
-          "env"
-      | OFile ->
-          "file"
-      | OEnvOverride ->
-          "envoverride"
-      | OCommand ->
-          "command"
-      | OOverride ->
-          "override"
-      | OAutomatic ->
-          "automatic"
-      | OInvalid ->
-          "invalid"
-    in
-    let flavor =
-      match v.v_flavor with
-      | FBogus ->
-          "bogus"
-      | FSimple ->
-          "simple"
-      | FRecursive ->
-          "recursive"
-      | FAppend ->
-          "append"
-      | FConditional ->
-          "conditional"
-    in
-    Printf.sprintf "name: %s\nvalue: %s\norigin: %s\nflavor: %s\n" name value
-                                                                origin flavor
-  in
-
-  String.concat "\n" (L.map debug_var d.d_variables)
-
-(** Return a string containing the rule section of the given databse, including
-    all information from the rule type. *)
-let pp_ast_rules (d : database) : string =
-  let debug_rule (r : rule) : string =
-    let next =
-      "Next: " ^
-      match r.r_next with
-      | Some rule ->
-          String.concat ", " rule.r_targets
-      | None ->
-          "None"
-    in
-    let targets =
-      "Targets: " ^ (String.concat ", " r.r_targets)
-    in
-    let suffixes =
-      "Suffixes: " ^ (String.concat ", " r.r_suffixes)
-    in
-    let deps =
-        "Dependencies:\n" ^
-                    (indent_1 (String.concat "\n" (L.map dep_to_s r.r_deps)))
-    in
-    let commands =
-      "Commands:\n" ^ (indent_1 (String.concat "\n" r.r_commands))
-    in
-    let num_targets = "Number of targets: " ^ P.string_of_int r.r_num in
-    let is_terminal = "Terminal: " ^ P.string_of_bool r.r_terminal in
-    let is_in_use = "Is in use: " ^ P.string_of_bool r.r_in_use in
-
-    String.concat "\n" [
-      "==========";
-      targets; next; suffixes; deps; commands;
-      num_targets; is_terminal; is_in_use;
-      "==========";
-    ]
-  in
-
-  String.concat "\n\n" (L.map debug_rule d.d_implicit_rules)
-
-(** Return a pretty printed version of the database. *)
-let pp_database (d : database) : string =
-  let var_header = indent "Variables:" in
-  incr d_indent;
-  let vars = indent (pp_ast_vars d) in
-  decr d_indent;
-
-  let rule_header = indent "Rules:" in
-  incr d_indent;
-  let rules = indent (pp_ast_rules d) in
-  decr d_indent;
-  String.concat "\n" [var_header; vars; rule_header; rules]
-
-
-(** Print the ast to stdout. The output shows the structure of the ast and can
-    not be used with GNU make (in contrast to the pp_... functions. *)
-let pp_ast (a : ast) : string =
-  d_indent := 1;
-  let dbs = String.concat "\n" (L.map pp_database a) in
-  "AST:\n" ^ dbs
   | FConditional
 
 (** Represent a variable definition (name, value, origin, and flavor). *)
-type variable
+type variable = {
+  v_name : string;
+  v_value : string;
+  v_origin : variable_origin;
+  v_flavor : variable_flavor;
+}
 
 (** Dependency. TODO. *)
 type dependency
 (** Convert a rule to a string in make database format. *)
 val s_of_rule : rule -> string
 
-(** Convert the rule targets to a string. *)
+(** Get a string representation of the targets of a rule. *)
 val s_of_rule_targets : rule -> string
 
+(** Get the string representation of a dependency. *)
+val s_of_dep : dependency -> string
+
+(** Get the string representation of a command. *)
+val s_of_command : command -> string
+
 (** {2 Initialization} *)
 (** Create a var instance of given inputs. *)
 val make_var :
 (** Return a string of the files section. The returned string is in valid
     make data base format. *)
 val pp_files : database -> string
-
-(** {2 Debug} *)
-(** Print the ast to stdout. The output shows the structure of the ast and can
-    not be used with GNU make (in contrast to the pp_... functions. *)
-val pp_ast : ast -> string

File src/debug.ml

+(** Debug
+
+    @file debug.ml
+    @author Claudio Corrodi
+    *)
+
+module L = List
+module S = Str
+module P = Pervasives
+open Ast
+
+(** {2 String Functions} *)
+(** Denote by how many tabs a line should be indented when using indent. *)
+let d_indent : int ref = ref 0
+
+(** Indent each line in s by i tabs. *)
+let indent_num (i : int) (s : string) : string =
+  S.global_replace (S.regexp "^.*$") (String.make i '\t' ^ "\\0") s
+
+(** Indent all lines by d_indent tabs. *)
+let indent (s : string) : string =
+  indent_num !d_indent s
+
+(** Indent by one tab. *)
+let indent_1 (s : string) : string =
+  indent_num 1 s
+
+
+(** {2 Debug} *)
+(** Return a string containing the variable section of the given database,
+    including all information from the variable type. *)
+let debug_pp_vars (d : database) : string =
+  let debug_var (v : variable) : string =
+    let name = v.v_name in
+    let value = v.v_value in
+    let origin =
+      match v.v_origin with
+      | ODefault ->
+          "default"
+      | OEnv ->
+          "env"
+      | OFile ->
+          "file"
+      | OEnvOverride ->
+          "envoverride"
+      | OCommand ->
+          "command"
+      | OOverride ->
+          "override"
+      | OAutomatic ->
+          "automatic"
+      | OInvalid ->
+          "invalid"
+    in
+    let flavor =
+      match v.v_flavor with
+      | FBogus ->
+          "bogus"
+      | FSimple ->
+          "simple"
+      | FRecursive ->
+          "recursive"
+      | FAppend ->
+          "append"
+      | FConditional ->
+          "conditional"
+    in
+    Printf.sprintf "name: %s\nvalue: %s\norigin: %s\nflavor: %s\n" name value
+                                                                origin flavor
+  in
+
+  String.concat "\n" (L.map debug_var d.d_variables)
+
+(** Return a string with a pretty printed representation of the given rule. *)
+let debug_rule (r : rule) : string =
+  let next =
+    "Next: " ^
+    match r.r_next with
+    | Some rule ->
+        String.concat ", " rule.r_targets
+    | None ->
+        "None"
+  in
+  let targets =
+    "Targets: " ^ (String.concat ", " r.r_targets)
+  in
+  let suffixes =
+    "Suffixes: " ^ (String.concat ", " r.r_suffixes)
+  in
+  let deps =
+      "Dependencies:\n" ^
+                  (indent_1 (String.concat "\n" (L.map s_of_dep r.r_deps)))
+  in
+  let commands =
+    "Commands:\n" ^ (indent_1 (String.concat "\n"
+                               (L.map s_of_command r.r_commands)))
+  in
+  let num_targets = "Number of targets: " ^ P.string_of_int r.r_num in
+  let is_terminal = "Terminal: " ^ P.string_of_bool r.r_terminal in
+  let is_in_use = "Is in use: " ^ P.string_of_bool r.r_in_use in
+
+  String.concat "\n" [
+    "==========";
+    targets; next; suffixes; deps; commands;
+    num_targets; is_terminal; is_in_use;
+    "==========";
+  ]
+
+(** Return a string containing the rule section of the given database,
+    including all information from the rule type. *)
+let debug_pp_rules (d : database) : string =
+  let count = L.length d.d_implicit_rules in
+  let cs = Printf.sprintf "# of implicit rules: %d\n" count in
+  let rules = String.concat "\n\n" (L.map debug_rule d.d_implicit_rules) in
+  cs ^ rules
+
+(** Return a string containing the files section of the given database. *)
+let debug_pp_files (d : database) : string =
+  let count = L.length d.d_files in
+  let cs = Printf.sprintf "# of files: %d\n" count in
+  let rules = String.concat "\n\n" (L.map debug_rule d.d_files) in
+  cs ^ rules
+
+(** Return a pretty printed version of the database. *)
+let debug_database (d : database) : string =
+  let var_header = indent "Variables:" in
+  incr d_indent;
+  let vars = indent (debug_pp_vars d) in
+  decr d_indent;
+
+  let rule_header = indent "Rules:" in
+  incr d_indent;
+  let rules = indent (debug_pp_rules d) in
+  decr d_indent;
+
+  let file_header = indent "Files:" in
+  incr d_indent;
+  let files = indent (debug_pp_files d) in
+  decr d_indent;
+
+  String.concat "\n" [var_header; vars;
+                      rule_header; rules;
+                      file_header; files;
+                     ]
+
+
+(** Print the ast to stdout. The output shows the structure of the ast and can
+    not be used with GNU make (in contrast to the pp_... functions. *)
+let debug_ast (a : ast) : string =
+  d_indent := 1;
+  let dbs = String.concat "\n" (L.map debug_database a) in
+  "AST:\n" ^ dbs
+

File src/debug.mli

+(** Debug
+
+    @file debug.mli
+    @author Claudio Corrodi
+    *)
+
+(** {2 Debug} *)
+(** Print the ast to stdout. The output shows the structure of the ast and can
+    not be used with GNU make (in contrast to the pp_... functions. *)
+val debug_ast : Ast.ast -> string

File src/ocamlmake.ml

    *)
 
 module A = Ast
+module D = Debug
 module S = Str
 module L = List
 
   "TODO"
 
 (** {2 Debug} *)
+(** Print the given ast to stdout. *)
 let debug_ast (a : A.ast) : unit =
-  Printf.printf "%s" (A.pp_ast a)
+  Printf.printf "%s" (D.debug_ast a)

File src/ocamlmake.mli

 val ast_to_db_string : Ast.ast -> ?print_cmds:bool -> unit -> string
 
 (** {2 Debug} *)
+(** Print the given ast to stdout. *)
 val debug_ast : Ast.ast -> unit

File src/parser.mly

   | rule in_rules {
     $1 :: $2
   }
+  | "# Not a target" in_rules {
+    Printf.printf "Not a target.\n";
+    $2
+  }
   | COMMENT in_rules {
     $2
   }