caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Question on camlp4 3.10
@ 2007-07-26 13:46 Benedikt Grundmann
  2007-07-26 13:50 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 5+ messages in thread
From: Benedikt Grundmann @ 2007-07-26 13:46 UTC (permalink / raw)
  To: Caml-list

How can I get the name of the module where my syntax extension is
applied?  For simplicity assume that I would like to write a MODULE
macro similar to __FILE__ in c.

shell> cat > test.ml
let _ = print_endline MODULE

shell> ./test
Test
shell> cat > test2.ml
module M =
struct
  let _ = print_endline MODULE
end

shell> ./test2
Test.M


Cheers,

Bene



-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Question on camlp4 3.10
  2007-07-26 13:46 Question on camlp4 3.10 Benedikt Grundmann
@ 2007-07-26 13:50 ` Nicolas Pouillard
       [not found]   ` <9b415f950707260657v6f2f92c6s84e2334f8c34e137@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Pouillard @ 2007-07-26 13:50 UTC (permalink / raw)
  To: Benedikt Grundmann; +Cc: caml-list

Look at the Camlp4MacroParser extension, there is __FILE__.

Excerpts from Benedikt Grundmann's message of Thu Jul 26 15:46:26 +0200 2007:
> How can I get the name of the module where my syntax extension is
> applied?  For simplicity assume that I would like to write a MODULE
> macro similar to __FILE__ in c.
> 
> shell> cat > test.ml
> let _ = print_endline MODULE
> 
> shell> ./test
> Test
> shell> cat > test2.ml
> module M =
> struct
>   let _ = print_endline MODULE
> end
> 
> shell> ./test2
> Test.M
> 
> 
> Cheers,
> 
> Bene
> 

-- 
Nicolas Pouillard aka Ertai


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Question on camlp4 3.10
       [not found]   ` <9b415f950707260657v6f2f92c6s84e2334f8c34e137@mail.gmail.com>
@ 2007-07-26 14:04     ` Nicolas Pouillard
  2007-07-27 13:52       ` Benedikt Grundmann
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Pouillard @ 2007-07-26 14:04 UTC (permalink / raw)
  To: Benedikt Grundmann; +Cc: caml-list

Excerpts from Benedikt Grundmann's message of Thu Jul 26 15:57:29 +0200 2007:
> Thanks,
> 
> But that gives me only the first part of my example.  What about the
> second one?  Is there an easy way to do that?
> 

Ah,  sorry  I didn't read the second part. It's a lot harder to have it correct
unless  you  completely ignore "open". You should do that with a Camlp4 filter,
that  store  the  module  path while traversing module declaration in a topdown
way.

> 
> 2007/7/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > Look at the Camlp4MacroParser extension, there is __FILE__.
> >
> > Excerpts from Benedikt Grundmann's message of Thu Jul 26 15:46:26 +0200 2007:
> > > How can I get the name of the module where my syntax extension is
> > > applied?  For simplicity assume that I would like to write a MODULE
> > > macro similar to __FILE__ in c.
> > >
> > > shell> cat > test.ml
> > > let _ = print_endline MODULE
> > >
> > > shell> ./test
> > > Test
> > > shell> cat > test2.ml
> > > module M =
> > > struct
> > >   let _ = print_endline MODULE
> > > end
> > >
> > > shell> ./test2
> > > Test.M
> > >
> > >
> > > Cheers,
> > >
> > > Bene
> > >
> >
> > --
> > Nicolas Pouillard aka Ertai
> >
> 

-- 
Nicolas Pouillard aka Ertai


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Question on camlp4 3.10
  2007-07-26 14:04     ` Nicolas Pouillard
@ 2007-07-27 13:52       ` Benedikt Grundmann
  2007-08-09 18:32         ` Nicolas Pouillard
  0 siblings, 1 reply; 5+ messages in thread
From: Benedikt Grundmann @ 2007-07-27 13:52 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml-list

Where can I find more documentation on camlp4 filter?  I assumed that
I should be able to use Camlp4FoldGenerator, but I already failed at
importing it into the toplevel, to interactively explore it:

bene@discworld:trunk$ rlwrap ocaml
        Objective Caml version 3.10.0

# #load "camlp4orf.cma";;
	Camlp4 Parsing version 3.10.0

# #load "camlp4lib.cma";;
# open Camlp4.PreCast;;
# class fold = Camlp4Filters.GenerateFold.generated;;
[tons of #load camlp4*.cma which all didn't get me Camlp4Filters into
the toplevel]


And another question, how should one handle errors in a syntax
extension?  One of my syntax extension handles only part of the full
ocaml type language, but instead of rewriting the type parser, I use
the original parser and apply a function on the Camlp4.Ast, so
sometimes I encounter a node in the ast that I cannot handle, what
should I do then?

Currently I do:

match t with
  | <:ctyp< ... > -> ...
  | _ ->
       let _loc = Ast.loc_of_ctyp t in
        Printf.eprintf "pa_message: Don't know how to handle the type
at %s\n%!"
          (Loc.to_string _loc);
        exit 1

But I'm sure that there is a better builtin way?!  (For example it
would be nice if I could
include a textual representation of t in the error message)


Thanks in advance,

Bene


2007/7/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> Ah,  sorry  I didn't read the second part. It's a lot harder to have it correct
> unless  you  completely ignore "open". You should do that with a Camlp4 filter,
> that  store  the  module  path while traversing module declaration in a topdown
> way.
>
> >
> > 2007/7/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > > Look at the Camlp4MacroParser extension, there is __FILE__.
> > >
> > > Excerpts from Benedikt Grundmann's message of Thu Jul 26 15:46:26 +0200 2007:
> > > > How can I get the name of the module where my syntax extension is
> > > > applied?  For simplicity assume that I would like to write a MODULE
> > > > macro similar to __FILE__ in c.
> > > >
> > > > shell> cat > test.ml
> > > > let _ = print_endline MODULE
> > > >
> > > > shell> ./test
> > > > Test
> > > > shell> cat > test2.ml
> > > > module M =
> > > > struct
> > > >   let _ = print_endline MODULE
> > > > end
> > > >
> > > > shell> ./test2
> > > > Test.M
> > > >
> > > >
> > > > Cheers,
> > > >
> > > > Bene
> > > >
> > >
> > > --
> > > Nicolas Pouillard aka Ertai
> > >
> >
>
> --
> Nicolas Pouillard aka Ertai
>


-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Question on camlp4 3.10
  2007-07-27 13:52       ` Benedikt Grundmann
@ 2007-08-09 18:32         ` Nicolas Pouillard
  0 siblings, 0 replies; 5+ messages in thread
From: Nicolas Pouillard @ 2007-08-09 18:32 UTC (permalink / raw)
  To: Benedikt Grundmann; +Cc: Inria Ocaml Mailing List

Excerpts from Benedikt Grundmann's message of Fri Jul 27 15:52:43 +0200 2007:
> Where can I find more documentation on camlp4 filter?  I assumed that
> I should be able to use Camlp4FoldGenerator, but I already failed at
> importing it into the toplevel, to interactively explore it:
> 
> bene@discworld:trunk$ rlwrap ocaml
>         Objective Caml version 3.10.0
> 
> # #load "camlp4orf.cma";;
>     Camlp4 Parsing version 3.10.0
> 
> # #load "camlp4lib.cma";;
> # open Camlp4.PreCast;;
> # class fold = Camlp4Filters.GenerateFold.generated;;
> [tons of #load camlp4*.cma which all didn't get me Camlp4Filters into
> the toplevel]
> 

Here is an ocaml *toplevel* session that use the fold generator (it's simpler to use it directly).

Note that's the CVS version (release310 branch).

$ rlwrap ocaml camlp4orf.cma camlp4lib.cma -I +camlp4/Camlp4Filters Camlp4FoldGenerator.cmo
        Objective Caml version 3.10.1+dev0 (2007-05-21)

        Camlp4 Parsing version 3.10.1+dev0 (2007-05-21)

# open Camlp4.PreCast;;
# let _loc = Loc.ghost;;val _loc : Camlp4.PreCast.Loc.t = <abstr>
# module M = Camlp4FoldGenerator.Make(Camlp4.PreCast.AstFilters);;
...
# Camlp4.PreCast.Printers.OCaml.print_implem
   (M.processor#str_item
      <:str_item<
         type t = [ Var of var | App of t and t | Lam of var and t ]
         and var = string;
         class map = Camlp4FoldGenerator.generated;
      >>);;
type t = | Var of var | App of t * t | Lam of var * t and var = string;;
class map =
  object ((o : 'self_type))
    method string : string -> 'self_type = o#unknown;;
    method var : var -> 'self_type = o#string;;
    method t : t -> 'self_type =
      function
      | Var _x -> let o = o#var _x in o
      | App (_x, _x_i1) -> let o = o#t _x in let o = o#t _x_i1 in o
      | Lam (_x, _x_i1) -> let o = o#var _x in let o = o#t _x_i1 in o;;
    method unknown : 'a. 'a -> 'self_type = fun _ -> o;;
  end;;
- : unit = ()



> 
> And another question, how should one handle errors in a syntax
> extension?  One of my syntax extension handles only part of the full
> ocaml type language, but instead of rewriting the type parser, I use
> the original parser and apply a function on the Camlp4.Ast, so
> sometimes I encounter a node in the ast that I cannot handle, what
> should I do then?
> 
> Currently I do:
> 
> match t with
>   | <:ctyp< ... > -> ...
>   | _ ->
>        let _loc = Ast.loc_of_ctyp t in
>         Printf.eprintf "pa_message: Don't know how to handle the type
> at %s\n%!"
>           (Loc.to_string _loc);
>         exit 1

prefer raise an exception instead of exiting.

> 
> But I'm sure that there is a better builtin way?!  (For example it
> would be nice if I could
> include a textual representation of t in the error message)

You can instanciate the ocaml pretty printer and use the #ctyp method.

HTH

> 
> 
> 2007/7/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > Ah,  sorry  I didn't read the second part. It's a lot harder to have it correct
> > unless  you  completely ignore "open". You should do that with a Camlp4 filter,
> > that  store  the  module  path while traversing module declaration in a topdown
> > way.
> >
> > >
> > > 2007/7/26, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > > > Look at the Camlp4MacroParser extension, there is __FILE__.
> > > >
> > > > Excerpts from Benedikt Grundmann's message of Thu Jul 26 15:46:26 +0200 2007:
> > > > > How can I get the name of the module where my syntax extension is
> > > > > applied?  For simplicity assume that I would like to write a MODULE
> > > > > macro similar to __FILE__ in c.
> > > > >
> > > > > shell> cat > test.ml
> > > > > let _ = print_endline MODULE
> > > > >
> > > > > shell> ./test
> > > > > Test
> > > > > shell> cat > test2.ml
> > > > > module M =
> > > > > struct
> > > > >   let _ = print_endline MODULE
> > > > > end
> > > > >
> > > > > shell> ./test2
> > > > > Test.M
> > > > >
> > > > >
> > > > > Cheers,
> > > > >
> > > > > Bene
> > > > >
> > > >
> > > > --
> > > > Nicolas Pouillard aka Ertai
> > > >
> > >
> >
> > --
> > Nicolas Pouillard aka Ertai
> >
> 

-- 
Nicolas Pouillard aka Ertai


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-08-09 18:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-26 13:46 Question on camlp4 3.10 Benedikt Grundmann
2007-07-26 13:50 ` [Caml-list] " Nicolas Pouillard
     [not found]   ` <9b415f950707260657v6f2f92c6s84e2334f8c34e137@mail.gmail.com>
2007-07-26 14:04     ` Nicolas Pouillard
2007-07-27 13:52       ` Benedikt Grundmann
2007-08-09 18:32         ` Nicolas Pouillard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).