Skip to content
This repository
tag: v144
Fetching contributors…

Cannot retrieve contributors at this time

file 428 lines (339 sloc) 12.591 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
(*
    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/>.
*)
# Parser for register directivs

decls:{{
   (**
      Main Parser of bsl (trx)

      Parse all bsl directives from Ocaml, Javascript and Opa files.

      @author Mathieu Barbin
      @author Mehdi Bouaziz
   *)
   (**
      This module is the parser for bsl register directives.
      It uses some global variables as an imperative state of the parser, cf BslParserState
   *)
}}

{{

   (* depends *)
   module String = BaseString

   (* shorthands *)
   module B = BslTypes
   module Q = QmlAst
   module D = BslDirectives
   module F = BslIncludeFormats

   (* TODO: if nobody uses it, remove ! *)
   let path_name = function
     | [] -> ""
     | (cp, _)::_ -> cp

   let path_prefix = function
     | [] -> ""
     | (_, cp)::_ -> cp ^ "."

   module ParserState = BslRegisterParserState
   let error = ParserState.error
   let warning = ParserState.warning
   let pos = ParserState.make_pos
   module IFormat = BslIncludeFormats.IFormat

   let _warning_sharp = Hashtbl.create 10
   let warning_sharp c =
     if Hashtbl.mem _warning_sharp c then () else
       (Hashtbl.add _warning_sharp c () ;
        ((error "Illegal sharp item in string : \"#%s\"" c) : unit))

   let assert_var = function
     | B.TypeVar (_, typevar) -> typevar
     | b ->
         error "Syntax error,@ @{<bright>%a@} is not a type variable@\n" B.pp b

   let build_args impl typ args =
     let fail () = error
       "Arity error in ##args of function %S@\nthe type of the function is %a@\n"
       impl BslTypes.pp typ
     in
     match typ with
     | B.Fun (_, params, ret) -> (
         match List.combine_opt args params with
         | Some typed_args -> D.Args (impl, typed_args, ret)
         | None -> fail ()
       )
     | _ ->
         error
           "Arity error in ##args of primitive %S@\nA function is expected, but the type is %a@\n"
           impl BslTypes.pp typ
 }}

space <- [ \r\t]
spacing <- space* $_
sspacing <- space+ $_
name_char <- [\-a-zA-Z0-9_.]
name <- name_char+ $_
endoftext <- !.
anything <- .* $_
registerRawKey <-
  / "`" ((![`] .)+ $_) "`" {{ __2, true }}
  / (([a-zA-Z_] ([\'a-zA-Z_0-9])*) $_) {{ __1, false }}
  (* / ((![;:\\\[] !space .)+ $_) {{ __1, false }} *)

quote <- [\"] $_
squote <- [\'] $_
regexp_char_nosp <- [a-zA-Z_ *+\-.<>{}0-9$?\[\]^\'/]
regexp_char <- regexp_char_nosp / [ ]
regexp <-
  / regexp_char_nosp+ $_
  / [\"] (regexp_char+ $_) [\"] {{ __2 }}

dirtags_or_sspacing <-
  / sspacing some_dirtags_spacing? {{ Option.default [] __2 }}
  / some_dirtags_spacing

dirtags_or_spacing <- spacing some_dirtags_spacing? {{ Option.default [] __2 }}

dirtags_or_nothing <- (spacing some_dirtags_spacing {{ __2 }})? {{ Option.default [] __1 }}

some_dirtags_spacing <- some_dirtags spacing {{ __1 }}

some_dirtags <- "[" spacing (dirtags_list spacing {{ __1 }})? "]" {{ Option.default [] __3 }}

dirtags_list <- dirtags_tag spacing dirtags_sep_tags* {{ __1::__3 }}

dirtags_sep_tags <- dirtags_sep spacing dirtags_tag {{ __3 }}

dirtags_sep <- "," / ";"

dirtags_tag <- name (spacing ":" spacing dirtags_value {{ __4 }})?
  {{
     let () =
       (* a temporary hack to help the migration *)
       if __1 = "opaname"
       then warning "the tag @{<bright>opaname@} is deprecated, it is now the default behavior"
     in
     __1, __2
  }}

dirtags_value <-
  / any_quotes_with_string
  / !quote !squote ((!dirtags_sep !"]" .)* $_) {{ String.rtrim __3 }}

any_quotes_with_string <-
  / (= quotes_with_string quote)
  / (= quotes_with_string squote)

;let quotes_with_string q = q (= quoted_string q) q {{ __2 }}
;let quoted_string q = (= quoted_string_char q)* $_
;let quoted_string_char q = string_special_char / (!q .) $_

string_special_char <-
  / "\\#" {{ "#" }}
  / "\\n" {{ "\n" }}
  / "\\r" {{ "\r" }}
  / "\\t" {{ "\t" }}
  / "\\'" {{ "'" }}
  / "\\\"" {{ "\"" }}
  / "\\\\" {{ "\\" }}
  / "\\" (. $_) {{ ((error "Illegal backslash escape in string : %S@\n" __2) : unit); "\\" ^ __2 }}

# OpaLang directive


;+opalang : { (BslTags.parsed_t, BslDirectives.opalang_directive) BslDirectives.decorated_source_elt } <-
  / spacing "##" spacing opalang_dir spacing (";;" spacing)? endoftext
  {{
     match __4 with
     | Some (dirtags, dircontent) -> D.Directive (ParserState.make_pos(), dirtags, dircontent)
     | None -> D.Source (ParserState.make_pos(), "")
   }}

  / spacing "##" spacing ("format"/"include-type"/"include") anything
  {{
     error "Syntax error, in@ ##%s%s@\n" __4 __5
  }}

  / anything endoftext
  {{
     D.Source (ParserState.make_pos(), __1)
  }}

opalang_dir <-
  / "format" dirtags_or_sspacing name spacing ("=" spacing)? format_spec {{ IFormat.add __3 __6; Some (__2, D.FormatDefinition __3) }}
  / "include-type" dirtags_or_sspacing regexp {{ Some (__2, D.IncludeType (__3)) }}
  / "include" dirtags_or_sspacing format_spec spacing name {{ Some (__2, D.Include (__3, __5)) }}

format_spec <-
  / format_name_or_fmt spacing format_sep
  {{
     match __1 with
     | F.Ffmt (fprinter, _) -> F.Ffmt(fprinter, Some __3)

     | F.Fmt_const s -> F.Ffmt((fun (_, _, _) -> s), Some __3)

     | _ -> error "Separator modification can only be used with a @{<bright>format(function)@}@\n"
  }}

  / format_name_or_fmt

registerKey <- registerRawKey

format_name_or_fmt <-
  / format_name
  / format_fmt

format_sep <- any_quotes_with_string

untilendline <- .+ endoftext $_

format_name <- name
  {{
     match IFormat.find_opt __1 with
     | Some fmt -> fmt
     | None ->
         error "Unknown iformat @{<bright>%S@}@\nHint:@\n@[<2>List of available iformats:@\n%a@]@\n"
           __1 IFormat.pp_show_format ()
  }}

format_fmt <- quote format_fmt_content quote {{ __2 }}

;+format_fmt_content : { BslIncludeFormats.fmt } <- format_fmt_char*
  {{
     List.fold_left IFormat.concat IFormat.empty __1
  }}

format_fmt_char <-
  / string_special_char {{ F.Fmt_const __1 }}
  / "#{" spacing format_iter_content spacing "}" {{ F.Mfmt [`Mfmt_iter __3] }}
  / "#{" {{ error "Parse error in iterative block" }}
  / "#m" {{ F.Mfmt [`Mfmt_name] }}
  / "#" format_fmt_repl {{ F.Ffmt(__2, None) }}
  / "#" (. $_) {{ warning_sharp __2; F.Fmt_const ("#" ^ __2) }}
  / (!quote . $_) {{ F.Fmt_const __1 }}

format_iter_content <- format_iter_elt_spacing* {{ IFormat.opt_list_to_iter __1 }}

format_iter_elt_spacing <- format_iter_elt spacing ("," spacing)? {{ __1 }}

format_iter_elt <-
  / "#rec" {{ None }}
  / format_name {{ Some __1 }}

format_fmt_repl <-
  / "n" {{ fun (n, _, _) -> n }}
  / "k" {{ fun (_, k, _) -> k }}
  / "t" {{ fun (_, _, t) -> t }}

# BypassLang directive

;+bypasslang : { (BslTags.parsed_t, BslDirectives.bypasslang_directive) BslDirectives.decorated_source_elt } <-
  / spacing "##" spacing bypasslang_dir spacing endoftext
     {{
        let dirtags, dircontent = __4 in
        D.Directive (ParserState.make_pos (), dirtags, dircontent)
     }}

  / spacing "##" spacing ("extern-type"/"opa-type"/"module"/"endmodule"/"register"/"args"/"property") anything
     {{
        error "Syntax error, in@ ##%s%s@\n" __4 __5
     }}

  / anything endoftext
     {{
        D.Source (ParserState.make_pos(), __1)
     }}

bypasslang_dir <-

  / "extern-type" dirtags_or_sspacing b_TypeVar_spaced_list? name b_TUPLE? spacing ("=" untilendline:source {{String.trim source}})?
     {{ __2,
        (* transition period: accept both prefix and postfix syntax for type parameters *)
        let typevars =
          match __3, __5 with
          | None, None -> []
          | Some vars, None ->
              warning "The prefix syntax for type parameters of extern-types is deprecated@\nPlease use the opa syntax (parenthesis and postfix)" ;
              vars
          | None, Some vars -> vars
          | Some _, Some _ ->
              error "Syntax error, you cannot use prefix and postfix syntax for type parameters"
        in
        let typevars = List.map assert_var typevars in
        D.ExternalTypeDef ( __4, typevars, __7)
     }}

  / "opa-type" dirtags_or_sspacing name b_TUPLE?
     {{ __2,
        let params = List.map assert_var (Option.default [] __4) in
        D.OpaTypeDef (__3, params)
     }}

  / "module" dirtags_or_sspacing registerKeys
  {{ __2, (
       match __3 with
       | skey, source_code, _ ->
           D.Module (skey, source_code)
     )
   }}

  / "endmodule" dirtags_or_nothing {{ __2, D.EndModule }}
  / "property" dirtags_or_nothing {{ __2, D.Property []}}

  / "register" dirtags_or_sspacing registerKeys spacing ":" spacing bslty
  {{ __2, (
       match __3 with
       | skey, source_code, injected ->
           D.Register (skey, source_code, injected, __7)
     )
  }}

  / args_directive
  {{
     let dirtags, arguments = __1 in
     match ParserState.get_last_directive () with
     | Some (D.Register (skey, none, injected, typ)) ->
         if Option.is_some none || injected
         then
           error (
             "@{<bright>##args@} can only be used just after a simple @{<bright>##register@} directive@\n"^^
             "The directive should declare one uniq key, without quote.@\n"^^
             "@[<2>@{<bright>Hint:@}@\nreplace the previous directive by:@\n"^^
             "##register %s : %a@]@\n"
           )
             skey
             BslTypes.pp typ
         else
           dirtags, build_args skey typ arguments
     | _ -> error "@{<bright>##args@} can only be used just after @{<bright>##register@}@\n"
  }}

args_directive <-
  / "args" dirtags_or_spacing args {{ __2, __3 }}

arg_name <- [a-zA-Z_] [a-zA-Z0-9_]* $_

comma_arg <- (',' spacing)? arg_name {{ __2 }}

args <-
  / "(" spacing arg_name spacing comma_arg* spacing ")" {{ __3 :: __5 }}
  / "(" spacing ")" {{ [] }}


registerKeys <-
  / registerRawKey spacing "\\" spacing registerRawKey
  {{
     match __1 with
     | key, protected ->
         if protected
         then error "@{<bright>##register %s@}@\nescaped keys are not allowed (only for impls)@\n" key
         else (
           match __5 with
           | impl, protected -> (key, Some impl, protected)
         )
  }}

  / registerRawKey
  {{
     let key, protected = __1 in
     if protected
     then error "@{<bright>##register %s@}@\nescaped keys are not allowed (only for impls)@\n" key
     else
       (key, None, false)
  }}

# BslTypes

;+bslty : { BslTypes.t } <- b_ENTRY {{ __1 }}

b_ENTRY <-
  / b_PARAMS spacing "->" spacing b_BASE {{ B.Fun (pos(), __1, __5) }}
  / spacing "->" spacing b_BASE {{ B.Fun (pos(), [], __4) }}
  / b_BASE

b_BASE <-
  / b_Const
  / b_TypeVar
  / b_Void
  / b_Bool
  / b_Option
  / b_OpaValue
  / b_External
  / "(" spacing b_ENTRY spacing ")" {{ __3 }}

b_Const <- (CintError / Cint / Cfloat / Cstring / Cchar ) {{ B.Const (pos(), __1) }}

CintError <- "int64" ![a-zA-Z]
  {{ error "@{<bright>int64@} are not supported for external primitives,@ use an @{<bright>int@} or use an @{<bright>external type@}@\n" }}
Cint <- "int" ![a-zA-Z] {{ Q.TyInt }}
Cfloat <- "float" ![a-zA-Z] {{ Q.TyFloat }}
Cstring <- "string" ![a-zA-Z] {{ Q.TyString }}
Cchar <- "char" ![a-zA-Z] {{ Q.TyChar }}

b_TypeVar <-
  / "'" name
  {{
     B.TypeVar (pos(), (ParserState.TypeVar.var __2))
  }}
  / '_' !name_char {{ B.TypeVar (pos(), ParserState.TypeVar.fresh ()) }}

b_Void <- "void" ![a-zA-Z] {{ B.Void (pos()) }}

b_Bool <- "bool" ![a-zA-Z] {{ B.Bool (pos()) }}

b_Option <-
  / "option(" spacing b_ENTRY spacing ")"
  {{
     B.Option (pos(), __3)
  }}

b_OpaValue <- "opa[" spacing b_ENTRY spacing "]" {{ B.OpaValue (pos(), __3) }}

b_External <-
 / name b_TUPLE? {{ B.External (pos(), __1, Option.default [] __2) }}


# Utils

b_TypeVar_spaced <- b_TypeVar spacing ","? spacing {{ __1 }}
b_TypeVar_spaced_list <-
  / "(" b_TypeVar_spaced* ")" spacing {{ __2 }}
  / b_TypeVar_spaced spacing {{ [__1] }}

b_PARAM <-
  / "(" spacing b_ENTRY spacing ")" {{ __3 }}
  / b_BASE

b_PARAMS <-
  / spacing b_PARAM spacing ("," spacing b_PARAM spacing {{ __3 }})* spacing {{ __2::__4 }}

b_TUPLE <- "(" b_PARAMS ")" {{ __2 }}
Something went wrong with that request. Please try again.