Article View: fa.caml
Article #76Re: camlp4 in toplevel
From: Max Mouratov
Date: Sun, 23 Sep 2012 00:09
Date: Sun, 23 Sep 2012 00:09
77 lines
4057 bytes
4057 bytes
On Friday, September 21, 2012 12:10:26 AM UTC+6, jdn wrote:
> I'm interested in using camlp4 for a DSL. To get up to speed I was hoping to use camlp4 in the toplevel to expand quotations to help with the learning curve. I can't seem to get this to work so I must be missing something, apologies if this is a very simple question. Here is a snippit of what I'm doing and the error:
>
>
>
> utop $ #camlp4o;;
>
> utop $ open Camlp4.PreCast;;
>
> utop $ let q = <:str_item< let f x = x >>;;
>
> Error: While finding quotation "str_item" in a position of "expr": There is no quotation expander available. Camlp4: Uncaught exception: Not_found
>
>
>
> I've tried many different things and done a google search but I can't figure out the problem. Any hints are greatly appreciated.
Greetings.
To be able to refer to the OCaml's str_item quotation you need to load the
corresponding findlib package (camlp4.quotations.o). An example of how to do it
in the vanilla toplevel:
# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
# #camlp4o;;
/usr/lib/ocaml/dynlink.cma: loaded
/usr/lib/ocaml/camlp4: added to search path
/usr/lib/ocaml/camlp4/camlp4o.cma: loaded
Camlp4 Parsing version 3.12.1
# #require "camlp4.quotations.o";;
/usr/lib/ocaml/camlp4/Camlp4Parsers/Camlp4QuotationCommon.cmo: loaded
/usr/lib/ocaml/camlp4/Camlp4Parsers/Camlp4OCamlOriginalQuotationExpander.cmo: loaded
# open Camlp4.PreCast;;
# let _loc = Loc.ghost;;
val _loc : Camlp4.PreCast.Loc.t = <abstr>
# <:str_item< 42 >>;;
- : Camlp4.PreCast.Ast.str_item =
Camlp4.PreCast.Ast.StSem (<abstr>,
Camlp4.PreCast.Ast.StExp (<abstr>, Camlp4.PreCast.Ast.ExInt (<abstr>, "42")),
Camlp4.PreCast.Ast.StNil <abstr>)
Notice how we first bind a dummy location to _loc, because the expanded
quotation expects _loc to be available in the current lexical context.
Have fun hacking your DSL with Camlp4, and be prepared to face a lot of bizarre
black magic, since the official Camlp4 documentation leaves much to be desired.
Just read the source, Luke, and get enlightenment (or maybe brain damage) :-)
Technical Details
Message-ID:
<29a24b20-6711-4326-bd9a-23bb15f83783@googlegroups.com>Path:
arc1-ca.pugleaf.net!e10ni13434282qan.0!nntp.google.com!l8no6250182qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailReferences:
<d4509b2c-c577-4566-965e-e624c28e32a0@googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: c0fe4b917e,8acb4113c0ae4e0f
X-Google-Attributes: gidc0fe4b917e,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.224.181.83 with SMTP id bx19mr3619260qab.1.1348358979113;
Sat, 22 Sep 2012 17:09:39 -0700 (PDT)
Received: by 10.52.30.164 with SMTP id t4mr1475821vdh.4.1348358979056; Sat, 22
Sep 2012 17:09:39 -0700 (PDT)
Path: arc1-ca.pugleaf.net!e10ni13434282qan.0!nntp.google.com!l8no6250182qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: fa.caml
Date: Sat, 22 Sep 2012 17:09:38 -0700 (PDT)
In-Reply-To: <d4509b2c-c577-4566-965e-e624c28e32a0@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.41.83.144; posting-account=h84scwoAAAA2_-2coYpAD4AwzeBGHYEz
NNTP-Posting-Host: 94.41.83.144
References: <d4509b2c-c577-4566-965e-e624c28e32a0@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <29a24b20-6711-4326-bd9a-23bb15f83783@googlegroups.com>
Subject: Re: camlp4 in toplevel
From: Max Mouratov <mmouratov@gmail.com>
Injection-Date: Sun, 23 Sep 2012 00:09:39 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable