Hi Martin,
Martin Jambon wrote:
> On Thu, 5 Apr 2007, Hugo Ferreira wrote:
>
>> Hello,
>>
>> I would like to use a reserved word and substitute that by a function
>> call. For example, the following:
>>
>> let _ = HEAP in
>> let h1 = HEAP in
>>
>> would become
>>
>> let __heap = new_heap () in
>> let h1 = new_heap () in
>
> You shouldn't try to do this because the parser looks only one token ahead
> to make its decision. If you add a rule that starts from "let" (it has
> to), the token which enables the parser to select this rule is in position
> 3, so it comes too late. Camlp4 will not warn you about the conflict but
> fail during preprocessing because it will choose either the predefined
> "let" rule or yours without knowing if it's the right one.
>
> It's what I was trying to explain in that message [ocaml-developer 20]:
>
> http://groups.google.com/group/ocaml-developer/browse_frm/thread/532cfd6cc675e98b?hl=en
>
>
As I recall you had suggested I use braces/parentheses as tokens to
delimit the expressions I am interested in so that it would be easier to
identify those elements and process them. I take it this is what you
are referring to.
Anyway, using such a syntax extension looks confusing. I figured we
could use the "let" "=" and "HEAP" as the tokens because upper case
identifiers after a "=" are not used in Ocaml unless they are
constructors.
(Unfortunately, as you had foreseen above, after quite a bit of
experimentation I realized that the predefined camlp4 let rule - let
binding actually - was being used which resulted in an error because not
constructor HEAP was defined).
>
>
>> To do this I attempted (here I am not checking the identifier name):
>>
>> binding: [
>> [ v = LIDENT ; "=" ; "HEAP" -> <:expr<$lid:v$ = new_heap ()>> ]
>> ] ;
>
> You build an expression, but what you need is a pair (pattern, expression).
>
Ok. I finally got their.
>> My problem however is how to activate the rule above? I figured I need
>> something like:
>>
>> Pcaml.str_item : LEVEL "top" [
>> [ "let"; OPT "rec";
>> l = LIST1 binding SEP "and" ; "in" ->
>> <str_item:< declare $list:l$ end>>
>> ]
>> ];
>>
>> But this doesn't even compile. I then concentrated on using Pcaml's
>> let_binding function and see If I could simply reconstruct the
>> structured expression but failed. I also tried:
>>
>> Pcaml.expr: LEVEL "expr1" [
>> [
>> "let"; l = LIST1 Pcaml.let_binding SEP "and" ; "in" -> <:expr<$l$>>
>>
>> Here I get the error:
>> This expression has type (MLast.patt * MLast.expr) list
>
> That's correct, it's what let_binding returns.
>
I looked at Hendrik Tew's original syntax quotations and verified that
in fact we are processing the let binding as if it is a function binding
hence the pattern. Confusing to me because of my lack of knowledge on
the AST - Ocaml syntax mapping.
>> but is here used with type MLast.expr
>
> let-in constructs work with a list of (pattern, expression) and one
> expression.
>
> let p1 = e1
> and p2 = e2
> in e
>
> Your quotation should look like:
>
> <:expr< let $list:l$ in $e$ >>
>
So far so good, but my problem is how do I convert "l" in the "$list:l$"
so that I can manipulate the "px = ex" assignments and only do this
when "ex" is "HEAP"?
I finally came up with this by (over) "simplifying" Hendrik Tews code:
GLOBAL: Pcaml.expr Pcaml.str_item ;
Pcaml.expr: LEVEL "expr1" [
[
"let"; o = OPT "rec";
l = LIST1 my_let_binding SEP "and"; "in";
x = Pcaml.expr LEVEL "top"
-> <:expr< let $opt:o2b o$ $list:l$ in
$x$ >>
....
my_let_binding:
[
[
p = "_" ; "=" ; "HEAP" -> <:patt< s >>, <:expr<Logic.new_heap ()>>
| p = LIDENT ; "=" ; "HEAP" -> <:patt< $lid:p$ >>,
<:expr<Logic.new_heap ()>>
]
] ;
As you can see I use my own "let_binding" that only succeeds with the
"HEAP" keyword. If this fails, camlp4 should fall back on the default or
predefined rule and compile as usual.
This seems to work ok and is what I was looking for.
Comments?
Hugo F.
>
>
> Martin
>
> --
> Martin Jambon
> http://martin.jambon.free.fr
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html
-~----------~----~----~----~------~----~------~--~---