nested_strings.ml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. open StdLabels
  2. let identifier = "escaped_string"
  3. let description = "Check for unnecessary use of expression encoded in string"
  4. let is_global = false
  5. let active = ref true
  6. type context = unit
  7. let initialize = Fun.id
  8. let finalize () = []
  9. module TypeBuilder = Compose.Expression (Get_type)
  10. module Expression = TypeBuilder.Make (struct
  11. type t = Report.t list
  12. type t' = Report.t list
  13. let v : Get_type.t Lazy.t * t -> t' = snd
  14. (** Identify the expressions reprented as string. That’s here that the report
  15. are added.
  16. All the rest of the module only push thoses warning to the top level. *)
  17. let literal :
  18. S.pos -> (Get_type.t Lazy.t * t) T.literal list -> Get_type.t Lazy.t -> t
  19. =
  20. fun pos content _type_of ->
  21. match content with
  22. | [ T.Expression (t', _); T.Text "" ] -> (
  23. match Get_type.get_type (Lazy.force t') with
  24. | Get_type.Integer -> []
  25. | _ ->
  26. let msg = Report.debug pos "This expression can be simplified" in
  27. [ msg ])
  28. | _ -> []
  29. let ident :
  30. (S.pos, Get_type.t Lazy.t * t) S.variable -> Get_type.t Lazy.t -> t =
  31. fun variable _type_of ->
  32. match variable.index with None -> [] | Some (_, t) -> t
  33. let integer : S.pos -> string -> Get_type.t Lazy.t -> t =
  34. fun pos t _type_of ->
  35. ignore pos;
  36. ignore t;
  37. []
  38. let function_ :
  39. S.pos ->
  40. T.function_ ->
  41. (Get_type.t Lazy.t * t) list ->
  42. Get_type.t Lazy.t ->
  43. t =
  44. fun pos f expressions _type_of ->
  45. ignore pos;
  46. ignore f;
  47. let exprs =
  48. List.fold_left ~init:[] expressions ~f:(fun acc el ->
  49. List.rev_append (snd el) acc)
  50. in
  51. exprs
  52. let uoperator :
  53. S.pos -> T.uoperator -> Get_type.t Lazy.t * t -> Get_type.t Lazy.t -> t =
  54. fun pos op r _type_of ->
  55. ignore op;
  56. ignore pos;
  57. snd r
  58. let boperator :
  59. S.pos ->
  60. T.boperator ->
  61. Get_type.t Lazy.t * t ->
  62. Get_type.t Lazy.t * t ->
  63. Get_type.t Lazy.t ->
  64. t =
  65. fun pos op (_, r1) (_, r2) _type_of ->
  66. ignore pos;
  67. ignore op;
  68. r1 @ r2
  69. end)
  70. module Instruction :
  71. S.Instruction with type t' = Report.t list and type expression = Expression.t' =
  72. struct
  73. type t = Report.t list
  74. (** Internal type used in the evaluation *)
  75. type t' = t
  76. let v : t -> t' = Fun.id
  77. type expression = Expression.t'
  78. let call : S.pos -> T.keywords -> expression list -> t =
  79. fun pos k exprs ->
  80. ignore pos;
  81. ignore k;
  82. List.concat exprs
  83. let location : S.pos -> string -> t = fun _ _ -> []
  84. let comment : S.pos -> t = fun _ -> []
  85. let expression : expression -> t = Fun.id
  86. let act : S.pos -> label:expression -> t list -> t =
  87. fun pos ~label instructions ->
  88. ignore pos;
  89. List.concat (label :: instructions)
  90. let fold_clause : (expression, t) S.clause -> t =
  91. fun (_pos1, expression, ts) -> List.concat (expression :: ts)
  92. let if_ :
  93. S.pos ->
  94. (expression, t) S.clause ->
  95. elifs:(expression, t) S.clause list ->
  96. else_:(S.pos * t list) option ->
  97. t =
  98. fun pos clause ~elifs ~else_ ->
  99. ignore pos;
  100. let init =
  101. match else_ with
  102. | None -> fold_clause clause
  103. | Some (_, ts) -> List.rev_append (fold_clause clause) (List.concat ts)
  104. in
  105. List.fold_left elifs ~init ~f:(fun t clause ->
  106. List.rev_append (fold_clause clause) t)
  107. let assign :
  108. S.pos ->
  109. (S.pos, expression) S.variable ->
  110. T.assignation_operator ->
  111. expression ->
  112. t =
  113. fun pos variable op expression ->
  114. ignore pos;
  115. ignore op;
  116. match variable.index with
  117. | None -> expression
  118. | Some v -> List.rev_append v expression
  119. end
  120. module Location = struct
  121. type t = Report.t list
  122. type instruction = Instruction.t'
  123. let v = Fun.id
  124. let location : unit -> S.pos -> instruction list -> t =
  125. fun () pos intructions ->
  126. ignore pos;
  127. List.concat intructions
  128. end