Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

426 lines (336 sloc) 12.373 kB
(*
Copyright © 2011, 2012 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 <- (Cint / Cfloat / Cstring) {{ B.Const (pos(), __1) }}
Cint <- "int" ![a-zA-Z0-9] {{ Q.TyInt }}
Cfloat <- "float" ![a-zA-Z0-9] {{ Q.TyFloat }}
Cstring <- "string" ![a-zA-Z0-9] {{ Q.TyString }}
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 }}
Jump to Line
Something went wrong with that request. Please try again.