From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.2 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 38E84BC6B for ; Thu, 10 Jan 2008 10:30:39 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAKN0hUfAXQInh2dsb2JhbACQHQEBAQgKKZh4 X-IronPort-AV: E=Sophos;i="4.24,266,1196636400"; d="scan'208";a="21080825" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 10 Jan 2008 10:30:39 +0100 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m0A9UcH5013682 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 10 Jan 2008 10:30:38 +0100 X-IronPort-AV: E=Sophos;i="4.24,266,1196636400"; d="scan'208";a="5895852" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail2-relais-roc.national.inria.fr with SMTP; 10 Jan 2008 10:30:38 +0100 Received: by ausone.inria.fr (sSMTP sendmail emulation); Thu, _d Jan 2008 10:29:40 +0100 From: "Nicolas Pouillard" Content-Type: text/plain; charset=UTF-8 Cc: caml-list Subject: Re: [Caml-list] Dynamically switching lexer To: "till.varoquaux" References: <9d3ec8300801091418j37e72687v892bcc2b33dc83b4@mail.gmail.com> In-Reply-To: <9d3ec8300801091418j37e72687v892bcc2b33dc83b4@mail.gmail.com> Date: Thu, 10 Jan 2008 10:29:40 +0100 Message-Id: <1199956968-sup-4697@ausone.local> User-Agent: Sup/0.3 X-Miltered: at concorde with ID 4785E5BE.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; lexer:01 camlp:01 lexer:01 ocaml:01 parser:01 expr:01 syntax:01 expr:01 lexers:01 cheers:01 camlp:01 dynamically:01 dynamically:01 caml-list:01 curr:01 Excerpts from till.varoquaux's message of Wed Jan 09 23:18:57 +0100 2008: > I'm currently doing a toy DSEL, > Camlp4 pops to mind as a very nice way to embed it unfortunately the > standard lexer it comes with is not all that great for my language. > Although I do know how to use another lexer it comes with a catch: > antiquotation need to be properly lexed to string to be passed to the > standard ocaml parser as strings (I use > let expr_of_string = Gram.parse_string Syntax.expr_eoi > this seems simpler than what is shown in tutorials. Am I missing > something?) off course the easy way to delimit such strings > (respecting nested comments, escaped strings...) would be to use the > former lexer, It would need to be switched between both lexers, is > this all possibe?. > Cheers, > Till > You cannot easily change the camlp4 lexer dynamically, but when you get a quotation you get a string that you can lex with a different lexer. Their is only two restrictions: - ">>" is forbidden since it will end the quotation - if you start a new quotation inside, you must close it: '<' (':' ident)? ('@' locname)? '<' ... ">>" Here is the intersting part of the lexer --------------------------- and quotation c = parse | '<' (':' ident)? ('@' locname)? '<' { store c ; with_curr_loc quotation c ; parse quotation c } | ">>" { store c } | eof { err Unterminated_quotation (loc c) } | newline { update_loc c None 1 false 0 ; store_parse quotation c } | _ { store_parse quotation c } --------------------------- -- Nicolas Pouillard aka Ertai