Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Caml4p... help
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Caml4p... help
Hi,

On Wed, Oct 09, 2002 at 11:50:23AM +1000, Pietro Abate wrote:

> My goal is to transform this syntax 
>   str pc : [a;b;c]
> into a declaration like that.
>   let pc_str = `Forall [| `Rule a; `Rule b; `Rule c |] 

According to your 2nd message, you need a "let" at toplevel, not
a "let..in". In this case, it is not "expr" that you must extend
but "str_item": indeed, the "let" at toplevel is a structure item,
not an expression.

> ocamlc -c -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I /usr/lib/ocaml/camlp4 str.ml
> File "str.ml", line 18, characters 2-10:
> This expression has type unit but is here used with type MLast.expr

Like Jeff Henrikson said, it is due to the extraneous "|" in the first
rule of "str": you must not put one. It is interpreted as an empty rule,
returning "()". I recognize that it is a pitfall of the syntax of EXTEND.

A remark: the definition of your type "kind" is unuseful in this file.
It will be useful only in the files using this syntax extension.

For the LINDENT, instead of LIDENT, this is not detected at compile
time, but when you use your syntax expander by "camlp4o ./str.cmo"
it displays:

   Lexer initialization error:
   - the constructor "LINDENT" is not recognized by Plexer
   Uncaught exception: Failure("Grammar.extend")

In Jeff's answer the #load "camlp4o.cma";; at the beginning of the file
is unuseful: Camlp4 knows that it is camlp4. On the other hand, the
other loads "q_MLast.cmo" and "pa_extend.cmo" are good and useful, since
they allow you to compile your file just with "-pp camlp4o".

Here is my version of your file (tested):

----------------------------------------
#load "pa_extend.cmo";;
#load "q_MLast.cmo";;

open Pcaml;;

let str = Grammar.Entry.create gram "str";;

EXTEND
  str_item:
    [ [ "str"; n = LIDENT; ":"; s = str ->
        <:str_item< value $lid:n$ = $s$ >> ] ];
  str:
    [ [ n = LIDENT -> <:expr<`Rule $str:n$>>
      | "["; s = LIST1 str SEP ";"; "]" -> <:expr<`Forall [|$list:s$|] >>
      | "{"; s = LIST1 str SEP ";"; "}" -> <:expr<`First [|$list:s$|] >> ] ];
END;;
----------------------------------------

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners