S.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. end
  68. module type Location = sig
  69. type t
  70. type instruction
  71. type context
  72. val v : t -> Report.t list
  73. val location : context -> pos -> instruction list -> t
  74. end
  75. (** {1 Unified module used by the parser } *)
  76. module type Analyzer = sig
  77. val identifier : string
  78. (** Identifier for the module *)
  79. val description : string
  80. (** Short description*)
  81. val active : bool ref
  82. (** Is the test active or not *)
  83. val is_global : bool
  84. (** Declare the checker as global. It requires to run over the whole file and
  85. will be disabled if the application only check a single location.
  86. Also, the test will be disabled if a syntax error is reported during the
  87. parsing, because this tell that I haven’t been able to analyse the whole
  88. source code. *)
  89. type context
  90. (** Context used to keep information during the whole test *)
  91. val initialize : unit -> context
  92. (** Initialize the context before starting to parse the content *)
  93. module Expression : Expression
  94. module Instruction : Instruction with type expression := Expression.t'
  95. module Location :
  96. Location with type instruction := Instruction.t' and type context := context
  97. val finalize : context -> (string * Report.t) list
  98. end
  99. (** Helper module used in order to convert elements from the differents
  100. representation levels.
  101. Thoses functions are intended to be used in the menhir parser, in order to
  102. limit the code in the mly file.
  103. *)
  104. module Helper (E : sig
  105. type t
  106. (** Internal type used in the evaluation *)
  107. type t'
  108. (** External type used outside of the module *)
  109. val v : t -> t'
  110. end) : sig
  111. val variable : (pos, E.t) variable -> (pos, E.t') variable
  112. (** Convert a variable from the [Expression.t] into [Expression.t'] *)
  113. end = struct
  114. let variable : (pos, E.t) variable -> (pos, E.t') variable =
  115. fun variable -> { variable with index = Option.map E.v variable.index }
  116. end