nested_strings.ml 3.7 KB

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