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.0 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id E67ADBC37 for ; Wed, 1 Jul 2009 18:27:07 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AncCAKwrS0pQW+UCe2dsb2JhbACZCAEBFiQEtn6EEQU X-IronPort-AV: E=Sophos;i="4.42,325,1243807200"; d="scan'208";a="30514311" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 01 Jul 2009 18:26:41 +0200 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1MM2dp-00064R-Sp for caml-list@inria.fr; Wed, 01 Jul 2009 16:26:37 +0000 Received: from ks300734.kimsufi.com ([91.121.65.225]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 01 Jul 2009 16:26:37 +0000 Received: from sylvain by ks300734.kimsufi.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 01 Jul 2009 16:26:37 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Sylvain Le Gall Subject: Re: ocamllex and python-style indentation Date: Wed, 1 Jul 2009 16:26:27 +0000 (UTC) Message-ID: References: <7d8707de0906110557n6a1511a2k9f4f00827f954cb6@mail.gmail.com> <4A310A5B.9010404@ens-lyon.org> <7d8707de0906120120x10cc8fe0p54adbd189003f3da@mail.gmail.com> <1750DDE9-4FC6-4657-9687-58240F520A70@mpi-sws.org> <2a1a1a0c0906301913t1dce3eebie91c7cc94f015481@mail.gmail.com> <2a1a1a0c0907010702u19ad7ba6kd831a71fa1b7f2a5@mail.gmail.com> <4A4B700D.3060701@mpi-sws.org> <4A4B7DE7.3080606@mpi-sws.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ks300734.kimsufi.com User-Agent: slrn/pre1.0.0-2 (Linux) Sender: news X-Spam: no; 0.00; le-gall:01 ocamllex:01 rossberg:01 rossberg:01 lexbuf:01 lexbuf:01 lexer:01 recursive:01 failwith:01 lexing:01 lexeme:01 lexing:01 ocamllex:01 lexer:01 mll:01 On 01-07-2009, Andreas Rossberg wrote: > Sylvain Le Gall wrote: >> May I recommend you to write this in a more simple way: >> >> ------------------------------------------------------------------------- >> rule lex = >> parse eof { () } >> | "(*" { start := pos lexbuf; lexNestComment lexbuf; lex lexbuf } >> >> and lexNestComment = >> parse eof { error (loc 2) "unterminated comment" } >> | "(*" { lexNestComment lexbuf } >> | "*)" { () } >> | _ { lexNestComment lexbuf } >> ------------------------------------------------------------------------- >> > > Mh, I think in lexNestComment it should at least say > > | "(*" { lexNestComment lexbuf; lexNestComment lexbuf } > > > That might work. I am not sure how well the various lexer generators > handle arbitrary recursive invocations and reentrance, though. Have you > tried it? If it works, yes, that's certainly a nicer version. > Yes, you're right, here it is: ------------------------------------------------------------------------ { let start = ref 0 let error l s = failwith "toto" let loc length = let pos = !start in (pos, pos+length) let pos _ = 0 } rule lex = parse eof { () } | "(*" { start := pos lexbuf; lexNestComment lexbuf; lex lexbuf } | _ { print_string (Lexing.lexeme lexbuf); lex lexbuf } and lexNestComment = parse eof { error (loc 2) "unterminated comment" } | "(*" { lexNestComment lexbuf; lexNestComment lexbuf } | "*)" { () } | _ { lexNestComment lexbuf } { let chn = open_in "nested.ml" in lex (Lexing.from_channel chn); close_in chn } ------------------------------------------------------------------------ and nested.ml: ----------------------------------- (* Comment 1 *) "Comment1 ok";; (* Commenbt 2 (* test *) *) "Comment 2 ok";; (* totot ... (* (* *) (**) *)*) "Comment 3 ok";; ----------------------------------- and the result: ---------------------------------------------------- ocamllex lexer.mll 9 states, 260 transitions, table size 1094 bytes ocamlc -o lexer lexer.ml ./lexer "Comment1 ok";; "Comment 2 ok";; "Comment 3 ok";; ----------------------------------------------------- It works ;-) Regards, Sylvain Le Gall