From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id 7B3617ED45 for ; Sat, 23 Jun 2012 15:42:11 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwBAJbG5U/RVda2kGdsb2JhbAAqGqUdkEQIIgEBAQEJCQ0HFAQjghgBAQEEEgITGQE4AQMMAQUFCw0uIQESAQUBHAYTIodbAwsLKZpNCQOPFIRyJw1KiH4BBQyKQWOGAgOIR4xlgRKJcIMiPoFUgkk X-IronPort-AV: E=Sophos;i="4.77,462,1336341600"; d="scan'208";a="164143754" Received: from mail-ob0-f182.google.com ([209.85.214.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 23 Jun 2012 15:42:10 +0200 Received: by obbun3 with SMTP id un3so6338027obb.27 for ; Sat, 23 Jun 2012 06:42:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=j57H7Up/4ZojAjC0+dBuakeBVVxP/qU1OMNSC7FOphE=; b=imgUgqLSTn77MLLae1hleWvst9y+TGTCOYAFLFL7TevGgU37TuHtv6xUbvbQ1kFHcX 4LZawL7viiadSxq0YHX0b2PrPwH1Q9hfwoxfBuCUM05dYa7l2Zbm2GwebJvyYIzJKJNN t8a0+J8xYhznFtuf+9+V8Z4UhO9qd2K9kaWTi08JcuEta/0ipRet1PkRvDVoycTQcA+y L7h3I5ubjgYXwXDt+dudosv+EXsACuyxNUX/JZAhVdAteFhv4bk1nm6jrWN3PYR8AJKR AHiSZuaNHfotbPNfxhRA2hAI0WwkWpDNrnHQo5jXB6zO6ofm0vRPGRF9yaQhD3nZrgpL v0qA== Received: by 10.60.24.67 with SMTP id s3mr5876330oef.54.1340458929527; Sat, 23 Jun 2012 06:42:09 -0700 (PDT) MIME-Version: 1.0 Sender: aaron678@gmail.com Received: by 10.76.114.133 with HTTP; Sat, 23 Jun 2012 06:41:49 -0700 (PDT) In-Reply-To: References: From: Aaron Bohannon Date: Sat, 23 Jun 2012 07:41:49 -0600 X-Google-Sender-Auth: U69SlFOZE8slPM88H6uZMcr7LzY Message-ID: To: Gabriel Scherer Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] Compiling with camlp4 extensions Ah, yes. That is helpful. I had thought of trying to "extend" OCaml by replacing the grammar with a different one, although I didn't know exactly how to do it. Of course, it seemed obvious to me that I wouldn't be able to use my own lexer if I did that. I'm not sure if I will want to do that or not yet, but I was thinking I would just learn to do it that way so I'd have that flexibility if I need it. Unfortunately, the page stops short of explaining how to pursue that approach. :( - Aaron On Sat, Jun 23, 2012 at 3:42 AM, Gabriel Scherer wrote: > See the "full parser tutorial" in the Camlp4 wiki, it has information > for what, if I have correctly understood, is your use case, including > location handling. > =A0http://brion.inria.fr/gallium/index.php/Full_parser_tutorial > > On Sat, Jun 23, 2012 at 2:43 AM, Aaron Bohannon = wrote: >> Thanks for the reply. =A0The example is helpful. =A0However, I should ha= ve >> been more clear: I don't exactly want to write a syntax extension, per >> se. =A0Rather, I am trying to use camlp4 to parse a non-OCaml grammar >> and to generate an OCaml AST. =A0So the "Register.OCamlSyntaxExtension" >> functor doesn't seem like it will work for me. =A0Instead, I tried using >> "Printers.Ocaml.print_implem" in my "extension" code and everything >> works fine, except for error locations. =A0Of course, I realize this is >> because the AST is being printed and then re-parsed, but I don't know >> how to prevent it from being reparsed. =A0I looked through all the >> Camlp4 interfaces and thought that perhaps I need to use the function >> "Register.register_str_item_parser". =A0But I couldn't make that work. >> Either that's not the function I need or else I don't know how to use >> it -- I can't tell which. >> >> =A0- Aaron >> >> On Fri, Jun 22, 2012 at 10:36 AM, Gabriel Scherer >> wrote: >>> All nodes in a Camlp4 AST are annotated with location information; the >>> locations you get from the parser are correct, and it is your >>> responsibility, as an extension writer, to ensure that any new nodes >>> you generate also have (approximately) correct location information. >>> >>> If you build AST nodes "by hand", you have to provide this location >>> explicitly. If you use the concrete syntax quotations, the location >>> used is the value _loc present in the environment, whatever it may be. >>> So to have correct locations, you have to make sure that, at every AST >>> you produce through a quotation, there is a "_loc" variable in scope >>> with the correct value. If you match AST pieces with quotation >>> patterns (match e with <:expr< $a$ + $b$ >> -> ...), you may bind the >>> location variable through the syntax "<:expr@foo<", for example: >>> (match e with <:expr@_loc< $a$ + $b$ >> -> ...). Finally, if you're >>> inside an EXTEND block defining a parsing rule, the idenfitier _loc is >>> implicitely bound to a location corresponding to what was parsed by >>> this rule. >>> >>> See for example the toy extension pa_refutable, that has example of >>> those various things: >>> =A0http://bluestorm.info/camlp4/pa_refutable.ml.html >>> >>> In some very rare cases (or if you are perfectionist), you may want to >>> give to a new node a location that is not quite the location of any of >>> the parsed node you're working on. You may use various functions of >>> the Loc submodule of your syntax definition to forge new locations; in >>> particular, Loc.merge merges two (supposed contiguous) locations. >>> =A0http://bluestorm.info/camlp4/camlp4-doc/Sig.Loc.html >>> >>> >>> >>> >>> >>> On Fri, Jun 22, 2012 at 5:53 PM, Aaron Bohannon wrote: >>>> Hi, >>>> >>>> I have been trying to use the new camlp4 to write an OCaml syntax >>>> extension.=A0 All the examples I have seen so far suggest that I use t= he >>>> extension by passing ocamlc the "-pp" option.=A0 But it seems that all= the >>>> location info for error messages gets lost when I do this unless I cat= ch and >>>> report the parse error myself within the extension.=A0 Is there some w= ay to >>>> get ocamlc to report the parse error at the correct location automatic= ally? >>>> >>>> - Aaron