S.ml 4.3 KB

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