Skip to content
This repository
tag: v454
Fetching contributors…

Cannot retrieve contributors at this time

file 309 lines (243 sloc) 7.11 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(**
Definition of register directives for building external primitives libraries.

@author Mathieu Barbin
*)

(**
This module defines the type of the preprocess directives you can find in
the different files given to {b bslregister}, for example :
{[
##register myfun : int, string -> string
]}

As a complement, see also the module [BslTags.t] which manage only the optionnal
parameters for directives :
{[
##register [tag:attribute] ....
like in :
##register [noprojection] ....
]}

For extanding tags, it is really easy, you just have to modify 1 file : BslTags.

For extanding directives, it is a little bit more work, you have to modify this
file, the parser of course, and all implementation of Generation for any language.
- BslOcaml
- BslJs
*)

(** {6 Alias, just for lisibility} *)

type pos = FilePos.pos
type source_code = string
type filename = string
type regexp = string
type injected = bool
type bslkey = string
type path = string
type skey = string



(** {6 Generic Types} *)
(**
The generic type of a directive.

Tags are some extra options you can give to the directive,
as in
{[
##mydirective [mytag1, mytag:with_1_argument] directive_contents....
]}
*)

type ('tags, 'directive) decorated_source_elt =
  | Directive of pos * 'tags * 'directive
  | Source of pos * source_code


type ('tags, 'directive) decorated_file = {
  filename : filename ;
  decorated_source : ('tags, 'directive) decorated_source_elt list
}

(** {6 Positions} *)

(**
This ast follows the guidelines about positions,
for error reporting.
*)

(** *)
let pos = LangAst.pos
let reset_pos = LangAst.reset_pos
let merge_pos = LangAst.merge_pos


(** {6 GUIDELINES for matching a [decorated_source_elt]} *)
(**

Verbose:
{[
| Directive (pos, tags, directive)
| Source (pos, source)
]}

Shorter:
{[
| Directive (p, t, d)
| Source (p, s)
]}
*)


(** {6 Opa files directives} *)
(**
Note:
There are no position in this AST, because such a directive
is always wrapped into a [decorated_source_elt] which already
provides the position.
*)
(** *)
type opalang_directive =
  | IncludeType of regexp
  | Include of BslIncludeFormats.fmt * path
  | FormatDefinition of string

(**
Specialization of the generic [decorated_file] for opa files.
*)
type opalang_decorated_file =
    (BslTags.t, opalang_directive) decorated_file

(**
{9 IncludeType}

A directive for producing all types definitions matching a regexp.
A typicall example, is to include all type from a given path

{[
##extern-type mymodule.*
]}
is represented as :
{[
IncludeType "mymodule.*"
]}

{9 Include}

Inclusion preprocess is a mechanism for producting directly code from
bypass definitions, without rewritting the code with the type manually.

TODO: document format, cf BslIncludeFormat.
{[
##include <format> path
]}

{9 FormatDefinition}
TODO: documentation
*)

(** {6 GUIDELINES for matching a [opalang_directive]} *)
(**

Verbose:
{[
| IncludeType regexp
| Include (iformat, path)
| FormatDefinition name
]}

Shorter:
{[
| IncludeType rgx
| Include (ifmt, p)
| FormatDefinition n
]}
*)


(** {6 Bypass files directives} *)
(**
Note:
There are no position in this AST, because such a directive
is always wrapped into a [decorated_source_elt] which already
provides the position.
*)
(** *)
type bypasslang_directive =
  | ExternalTypeDef of skey * BslTypes.typevar list * source_code option
  | OpaTypeDef of skey * BslTypes.typevar list
  | Module of skey * source_code option
  | EndModule
  | Register of skey * source_code option * injected * BslTypes.t
  | Args of skey * (string * BslTypes.t) list * BslTypes.t
  | Property of BslTags.parsed_t

(**
Specialization of the generic [decorated_file] for Ocaml and Javascript files.
*)
type bypasslang_decorated_file =
    (BslTags.t, bypasslang_directive) decorated_file

(**
{9 ExternTypeDef}

A extern type definition.

This makes actually sence only in Ocaml files.
It introduces a new ocaml type.

{[
##extern-type ('a, 'b, 'c) toto = Ka of 'a | Kb of 'b | Kc of 'c
]}
is represented as :
{[
ExternTypeDef ("toto", ['a ; 'b ; 'c], "Ka of 'a | Kb of 'b | Kc of 'c")
]}

@see "BslTypesGeneration.Ocaml" to see how this type is defined in opa, and how it is printed
in the [ml] and [mli] of the generated [MLRuntime]

{9 Module}

{[
##module skey \ impl_name
]}
is represented as :
{[
Module ("skey", "impl_name")
]}

{9 EndModule}

For closing a module previously open.

Syntax:
{[
##endmodule
]}

{9 Register}

For defining a new primitive.
+ The name of the key can be different than the name of the primitive in the
target language.
+ The primitive can be injected from an existing implementation. In this case,
the implementation will be directly injected in the generated code. (cf Syntax 3)

Syntax 1:
{[
##register skey : int, int -> int
]}
is represented as :
{[
Register ("skey", "skey", false, BslTypes.(int, int -> int))
]}

Syntax 2:
{[
##register skey \ different_name : int, int -> int
]}
is represented as :
{[
Register ("skey", "different_name", false, BslTypes.(int, int -> int))
]}

Syntax 3:
{[
##register skey \ `Pervasives.(+)` : int, int -> int
]}
is represented as :
{[
Register ("skey", "Pervasives.(+)", true, BslTypes.(int, int -> int))
]}

This is the only case where the injected bool is set to [true]

{9 Args}

TODO: documentation for args.
[impl], [arg_name, arg_type], return_type

{9 Property}

TODO: documentation for property
*)


(** {6 GUIDELINES for matching a [bypasslang_directive]} *)
(**

Verbose:
{[
| ExternalTypeDef (skey, params, implementation)
| Module (skey, implementation)
| EndModule
| Register (skey, implementation, injected, bslty)
| Args (name, args, bslty)
| Property (props)
]}

Shorter:
{[
| ExternalTypeDef (n, p, imp)
| Module (n, imp)
| EndModule
| Register (n, imp, inj, ty)
| Args (n, xs, ty)
| Property p
]}
*)
Something went wrong with that request. Please try again.