From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id B2EBEBBAF for ; Mon, 26 Jul 2010 18:30:37 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmUCACJVTUxKfVIukGdsb2JhbACfWAgVAQEBAQkJDAcRAx+0doViiQIBAQMFhTEEhAiEXIdh X-IronPort-AV: E=Sophos;i="4.55,262,1278280800"; d="scan'208";a="64435346" Received: from mail-ww0-f46.google.com ([74.125.82.46]) by mail1-smtp-roc.national.inria.fr with ESMTP; 26 Jul 2010 18:30:37 +0200 Received: by wwb18 with SMTP id 18so362240wwb.3 for ; Mon, 26 Jul 2010 09:30:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:sender:received :in-reply-to:references:from:date:x-google-sender-auth:message-id :subject:to:cc:content-type; bh=uiquUDNpjrcdJtDatdpzw8O1V6qS4ALtb48CAcQFfyQ=; b=ZIBk0lAL7aHchwvQwUCARA7xAi8/LoMnYXJ+Hl9bEM5al+WAAwSfNmHKBPPOkI7Jox gYDhJb1drSM88/Hyss6iP8lIFT2DxFX10wLAAVyMrV690SJm6MTIXeYxIeS9bG7ymuk8 cluK0yDA8hWnzVnpiOp4ARjH4ccCo0ot/CzKA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; b=Dr3KcYJ+fP7y9p4c3EmxGrlowqqH9+oeh/uSc1XnrRbjZ3dWKO+IM80dxGGmFexKBH qNhw7Q+KTa6supRVQYl0WR1jpb/cjwS0cl1yLl8WCFXte8yF9OsRq6ME9HugtVqmkkrK 4LO69325RzCiORByHTk6et5SGGRALwjfso1vI= Received: by 10.216.164.141 with SMTP id c13mr7589601wel.83.1280161836328; Mon, 26 Jul 2010 09:30:36 -0700 (PDT) MIME-Version: 1.0 Sender: jake.donham@gmail.com Received: by 10.216.186.17 with HTTP; Mon, 26 Jul 2010 09:30:16 -0700 (PDT) In-Reply-To: References: <4c4da611.e8e9d80a.53b5.5b50@mx.google.com> From: Jake Donham Date: Mon, 26 Jul 2010 09:30:16 -0700 X-Google-Sender-Auth: wTBQgeFmoAhn1hH55rcCkDs8MkM Message-ID: Subject: Re: [Caml-list] [Camlp4] Quotation expander with OCaml syntax To: Raphael Proust Cc: Nicolas Pouillard , caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; camlp:01 expander:01 ocaml:01 syntax:01 nodes:01 node:01 lexer:01 ocaml:01 nodes:01 nesting:01 parser:01 parser:01 camlp:01 implements:01 26,:98 On Mon, Jul 26, 2010 at 8:41 AM, Raphael Proust wrote: > What I basically need is to get an AST with antiquotations and quotations being > special nodes. How is this achievable w/o reimplementing a whole grammar? There is already a special node for antiquotations, but not quotations; see below. > The alternative solution is to use raw strings, to find antiquotation marks, to > split the string and to reinject it in the different files. Is there a way to > keep precise _loc information this way? This is more or less what already happens with quotations / antiquotations. The lexer reads the whole quotation into a string (checking that nested antiquotations / quotations are balanced, so it doesn't stop too soon, but not otherwise processing them). The string is then expanded (for OCaml AST quotations at least) by parsing it into an OCaml AST, then lifting the AST (see my previous response), then filtering the resulting AST to parsed antiquotations and insert conversions (e.g. $`int:i$ becomes ExInt (_, string_of_int i)). The nested parsing begins with the current location, so _loc information is kept precisely. Antiquotations are represented as special nodes in the OCaml AST containing the string (again, the whole antiquotation, even if there is further nesting), which are parsed during the AST filtering phase. I'm not sure what syntactic problems Nicolas suggests you would run into by reusing the existing parser and quotation mechanism. But it might be a bit hairy to implement. One issue is that you can't just drop antiquotations anywhere; where they can appear (and with what tag) is given by the parser. If you want to go in this direction, the place to start would be Camlp4QuotationCommon.ml, which implements OCaml AST quotations / antiquotations. You can see there that you can parse starting at arbitrary non-terminals, and you can filter the OCaml AST without a giant pattern match using the Ast.map object. Jake