PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/syntax/pa_trace.ml

http://github.com/avsm/mirage
OCaml | 89 lines | 51 code | 10 blank | 28 comment | 2 complexity | 832305c0f099305a71e0c93d6e4fd01c MD5 | raw file
Possible License(s): GPL-3.0, 0BSD, GPL-2.0, LGPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. (*
  2. * Copyright (c) 2008, Jeremie Dimino <jeremie@dimino.org>
  3. * Copyright (c) 2012, Anil Madhavapeddy <anil@recoil.org>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of the <organization> nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. *
  28. *)
  29. open Camlp4.PreCast
  30. let filter_std = true
  31. let output_fn = "print_endline"
  32. type col = Blue | Green | Cyan | Red
  33. let code_of_col = function |Blue -> 34 |Green -> 32 |Cyan -> 36 |Red -> 31
  34. let color col x = Printf.sprintf "\027[1;%dm%s\027[m" (code_of_col col) x
  35. let dir_col = function
  36. |"std" -> if filter_std then None else Some Blue
  37. |"os/unix" |"os/xen" -> Some Cyan
  38. |"net/direct"|"net/socket"|"net/direct/tcp" -> Some Green
  39. |_ -> Some Red
  40. let color_log _loc msg =
  41. let fname = Loc.file_name _loc in
  42. let dirname = Filename.dirname fname in
  43. match dir_col (Filename.dirname fname) with
  44. |None -> None
  45. |Some col -> Some (Printf.sprintf ">>> %s: %s" (color col msg) (Loc.to_string _loc))
  46. let add_debug_expr name e =
  47. let _loc = Ast.loc_of_expr e in
  48. match color_log _loc name with
  49. |None -> <:expr< $e$ >>
  50. |Some m -> <:expr< $lid:output_fn$ $str:m$; $e$ >>
  51. let rec map_match_case name = function
  52. | <:match_case@_loc< $m1$ | $m2$ >> ->
  53. <:match_case< $map_match_case name m1$ | $map_match_case name m2$ >>
  54. | <:match_case@_loc< $p$ when $w$ -> $e$ >> ->
  55. <:match_case< $p$ when $w$ -> $add_debug_expr name e$ >>
  56. | m ->
  57. m
  58. let rec map_expr name = function
  59. | <:expr@_loc< fun $p$ -> $e$ >> ->
  60. <:expr< fun $p$ -> $map_expr name e$ >>
  61. | <:expr@_loc< function $m$ >> ->
  62. <:expr< function $map_match_case name m$ >>
  63. | e ->
  64. add_debug_expr name e
  65. let rec map_binding = function
  66. | <:binding@_loc< $lid:func$ = fun $p$ -> $e$ >> ->
  67. <:binding< $lid:func$ = fun $p$ -> $map_expr func e$ >>
  68. | <:binding@_loc< $lid:func$ = function $m$ >> ->
  69. <:binding< $lid:func$ = function $map_match_case func m$ >>
  70. | <:binding@_loc< $a$ and $b$ >> ->
  71. <:binding< $map_binding a$ and $map_binding b$ >>
  72. | x ->
  73. x
  74. let map_str_item = function
  75. | Ast.StVal (_loc, rec_mode, binding) ->
  76. <:str_item< let $rec:rec_mode$ $map_binding binding$ >>
  77. | x ->
  78. x
  79. let () = AstFilters.register_str_item_filter (Ast.map_str_item map_str_item)#str_item