Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[enhance] opa syntax: in parser construct, add possibility to directl…

…y call a subparser with parameter
  • Loading branch information...
commit 503663ac35160b657b6c99d89163ddc19bc71dd2 1 parent 3c06874
@OpaOnWindowsNow OpaOnWindowsNow authored
View
19 opalang/syntax/opa_parser.trx
@@ -753,8 +753,8 @@ directive <-
*)
/** function calls, field access **/
function_call_dot <-
- / expr_before_mandatory_function_call:e function_arguments_dot_method_nosp+:params hint_end_of_callable not_assign
- {{ make_function2 letins double_dot (`expr e) params }}
+ / ident_call_dot
+ / paren_expr_call_dot
/ (=careful_deco(Opa_lexer.underscore_nosp)):p function_arguments_dot_method_nosp+:params hint_end_of_callable not_assign
{{ make_function2 letins double_dot (`hole (label p)) params }}
/ expr_before_possible_function_call:e ( function_arguments_dot_method_nosp+:params hint_end_of_callable not_assign
@@ -762,12 +762,23 @@ function_call_dot <-
/ _succeed
{{ fun e -> undecorate e }}):f {{ f e }}
+
(* Only certain kind of expression can be applied *)
-;expr_before_mandatory_function_call = careful_deco just_expr_before_mandatory_function_call
+let call_dot func = (=careful_deco(func)):e function_arguments_dot_method_nosp+:params hint_end_of_callable not_assign
+ {{ make_function2 letins double_dot (`expr e) params }}
;expr_before_possible_function_call = careful_deco just_expr_before_possible_function_call
-just_expr_before_mandatory_function_call <-
+
+ident_call_dot <- (=call_dot(just_ident_before_mandatory_function_call))
+paren_expr_call_dot <- (=call_dot(just_paren_expr_before_mandatory_function_call))
+
+just_ident_before_mandatory_function_call <-
/ ml_identifier_nosp:i {{ Ident i }}
+
+just_paren_expr_before_mandatory_function_call <-
/ lpar expr:e rpar {{ undecorate e }}
+
+
+
just_expr_before_possible_function_call <-
/ id
/ directive
View
8 opalang/syntax/parser_utils.ml
@@ -502,6 +502,14 @@ let declaration_directive dirs bindings =
* Utils on functions
*)
+(* get the ident or field in a general call (ident, call dot composition) *)
+let rec get_meaningful_called_ident (e,_) = match e with
+ | Ident(id) -> id
+ | Dot (_,field) -> field
+ | Apply(f,_) -> get_meaningful_called_ident f
+ | _ -> failwith "You are using autobind (i.e. ~) in an ambiguous way"
+
+
(* takes (arg1,arg2,...) and e and makes fun(arg1,arg2,...) -> e*)
let args_expr_to_lambda ?zero_ary args e =
if args = [] then
View
1  opalang/syntax/parser_utils.mli
@@ -164,6 +164,7 @@ val bool : bool -> annot -> ('a, 'b) coerced_expr
(** Lambda, application *)
+val get_meaningful_called_ident : (string, 'b) expr -> string
val args_expr_to_lambda : ?zero_ary:annot -> 'a pat list -> ('a, 'b) expr -> ('a, 'b) expr
val ( & ) : ('a, 'b) expr -> ('a, 'b) expr list -> ('a, 'b) expr
val ( &. ) : ('a, 'b) expr -> ('a, 'b) expr list -> ('a, 'b) expr_node
View
18 opalang/syntax/trx.trx
@@ -41,10 +41,10 @@ Item = deco(Item_aux)
Item_aux <-
/ SubruleName?:name Prefix:prefix Primary:primary Suffix:suffix
{{ {Trx_ast.item_name=name; Trx_ast.item_prefix=prefix; Trx_ast.item_primary=primary; Trx_ast.item_suffix=suffix} }}
-/ [~] (=deco(Identifier:e {{e}})):e Suffix:suffix
+/ [~] AutobindIdentifierCall:e Suffix:suffix
(* the only place when ~ is allowed is when saying ~p or ~X.p*, but not ~{X.p}, not ~[abc], etc. *)
- {{ let (((name,_),e),label) = e in
- {Trx_ast.item_name=Some name; Trx_ast.item_prefix=`NORMAL; Trx_ast.item_primary=(Trx_ast.Rule e,label); Trx_ast.item_suffix=suffix} }}
+ {{ let name,((_,label) as e) = e in
+ {Trx_ast.item_name=Some name; Trx_ast.item_prefix=`NORMAL; Trx_ast.item_primary=(Trx_ast.Rule e), label; Trx_ast.item_suffix=suffix} }}
ParserSeq = deco(ParserSeq_aux)
ParserSeq_aux <- pos:pos Item+:items ParserCode?:pc {{ {Trx_ast.seq_items=items; Trx_ast.seq_code=pc} }}
@@ -66,7 +66,7 @@ Suffix <- SuffixElement?:v {{ Option.default `NORMAL v }}
Primary = deco(Primary_aux)
Primary_aux <-
/ Opa_parser.opa_in_braces:e {{ Trx_ast.Code e }}
- / Identifier:e {{ Trx_ast.Rule (snd e) }}
+ / IdentifierCall:e {{ Trx_ast.Rule e }}
/ OPEN Expression:exp CLOSE {{ Trx_ast.Parens exp }}
/ Literal:lit TILDE?:cs {{ Trx_ast.Literal(lit, cs=None) }}
/ String_expr.string_with_opa:e {{ Trx_ast.DynamicLiteral e }}
@@ -88,9 +88,13 @@ Char <- Opa_lexer.char_char
{6 Lexical analysis}
*)
-Identifier <-
- / deco_ml_identifier:n ([.] deco_ml_identifier:s{{s}})+:l {{ List.last (n :: l), dots2 (n :: l) }}
- / deco_ml_identifier:n !EQUAL {{ n, unc2 ident n }}
+/** parser subrule call */
+IdentifierCall <-
+ / (=deco(Opa_parser.ident_call_dot))
+ / deco_ml_identifier:n !EQUAL {{ let s,a = n in (ident s a : ('a,'b) SurfaceAst.expr) }}
+
+/** parser subrule call with autobind */
+AutobindIdentifierCall <- IdentifierCall:e {{( get_meaningful_called_ident e, e : string * (string,'b) SurfaceAst.expr) }}
(* forbid braces [{...}] in literals, if a literal contains braces then we want to interpret
it as an embedded OPA expression. Literal braces need escaping. *)
Please sign in to comment.
Something went wrong with that request. Please try again.