S.ml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. (**
  2. This module describe the type an analyzer must implement in order to be
  3. used with the parser.
  4. The module is divided in three modules :
  5. - Expression : the finest part of the QSP syntax.
  6. - Instruction : if/act block,
  7. - Location
  8. *)
  9. (** {1 Generic types used in the module } *)
  10. type pos = Lexing.position * Lexing.position
  11. (** The type pos is used to track the starting and ending position for the
  12. given location. *)
  13. type ('a, 'b) variable = {
  14. pos : 'a;
  15. name : string;
  16. index : 'b option;
  17. local : bool;
  18. }
  19. (** Describe a variable, using the name in capitalized text, and an optionnal
  20. index.
  21. If missing, the index should be considered as [0].*)
  22. type ('a, 'b) clause = pos * 'a * 'b list
  23. (** {1 Checker Signature} *)
  24. (** Represent the evaluation over an expression *)
  25. module type Expression = sig
  26. type t
  27. (** Internal type used in the evaluation *)
  28. type t'
  29. (** External type used outside of the module *)
  30. val v : t -> t'
  31. val ident : (pos, t) variable -> t
  32. (*
  33. Basic values, text, number…
  34. *)
  35. val integer : pos -> string -> t
  36. val literal : pos -> t T.literal list -> t
  37. val function_ : pos -> T.function_ -> t list -> t
  38. (** Call a function. The functions list is hardcoded in lib/lexer.mll *)
  39. val uoperator : pos -> T.uoperator -> t -> t
  40. (** Unary operator like [-123] or [+'Text']*)
  41. val boperator : pos -> T.boperator -> t -> t -> t
  42. (** Binary operator, for a comparaison, or an operation *)
  43. end
  44. module type Instruction = sig
  45. type t
  46. (** Internal type used in the evaluation *)
  47. type t'
  48. (** External type used outside of the module *)
  49. val v : t -> t'
  50. type expression
  51. val call : pos -> T.keywords -> expression list -> t
  52. (** Call for an instruction like [GT] or [*CLR] *)
  53. val location : pos -> string -> t
  54. (** Label for a loop *)
  55. val comment : pos -> t
  56. (** Comment *)
  57. val expression : expression -> t
  58. (** Raw expression *)
  59. val if_ :
  60. pos ->
  61. (expression, t) clause ->
  62. elifs:(expression, t) clause list ->
  63. else_:(pos * t list) option ->
  64. t
  65. val act : pos -> label:expression -> t list -> t
  66. val assign :
  67. pos ->
  68. (pos, expression) variable ->
  69. T.assignation_operator ->
  70. expression ->
  71. t
  72. val for_ :
  73. pos ->
  74. (pos, expression) variable ->
  75. start:expression ->
  76. to_:expression ->
  77. step:expression option ->
  78. t list ->
  79. t
  80. (**
  81. for j = 1 to 5 step var:
  82. stri = 1 & strmax = 3
  83. for i = stri to strmax:
  84. end
  85. end
  86. *)
  87. end
  88. module type Location = sig
  89. type t
  90. type instruction
  91. type context
  92. val v : t -> Report.t list
  93. val location : context -> pos -> instruction list -> t
  94. end
  95. (** {1 Unified module used by the parser } *)
  96. module type Analyzer = sig
  97. val identifier : string
  98. (** Identifier for the module *)
  99. val description : string
  100. (** Short description*)
  101. val active : bool ref
  102. (** Is the test active or not *)
  103. val is_global : bool
  104. (** Declare the checker as global. It requires to run over the whole file and
  105. will be disabled if the application only check a single location.
  106. Also, the test will be disabled if a syntax error is reported during the
  107. parsing, because this tell that I haven’t been able to analyse the whole
  108. source code. *)
  109. type context
  110. (** Context used to keep information during the whole test *)
  111. val initialize : unit -> context
  112. (** Initialize the context before starting to parse the content *)
  113. module Expression : Expression
  114. module Instruction : Instruction with type expression := Expression.t'
  115. module Location :
  116. Location with type instruction := Instruction.t' and type context := context
  117. val finalize : context -> (string * Report.t) list
  118. end
  119. (** Helper module used in order to convert elements from the differents
  120. representation levels.
  121. Thoses functions are intended to be used in the menhir parser, in order to
  122. limit the code in the mly file.
  123. *)
  124. module Helper (E : sig
  125. type t
  126. (** Internal type used in the evaluation *)
  127. type t'
  128. (** External type used outside of the module *)
  129. val v : t -> t'
  130. end) : sig
  131. val variable : (pos, E.t) variable -> (pos, E.t') variable
  132. (** Convert a variable from the [Expression.t] into [Expression.t'] *)
  133. end = struct
  134. let variable : (pos, E.t) variable -> (pos, E.t') variable =
  135. fun variable -> { variable with index = Option.map E.v variable.index }
  136. end