caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Sylvain Le Gall <sylvain@le-gall.net>
To: caml-list@inria.fr
Subject: Re: ocamllex and python-style indentation
Date: Wed, 1 Jul 2009 16:26:27 +0000 (UTC)	[thread overview]
Message-ID: <slrnh4n3hj.hil.sylvain@gallu.homelinux.org> (raw)
In-Reply-To: <4A4B7DE7.3080606@mpi-sws.org>

On 01-07-2009, Andreas Rossberg <rossberg@mpi-sws.org> 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


  reply	other threads:[~2009-07-01 16:27 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-11 12:57 Andrej Bauer
2009-06-11 13:12 ` [Caml-list] " yoann padioleau
2009-06-11 13:21 ` Andreas Rossberg
2009-06-11 13:44 ` Martin Jambon
2009-06-12  8:20   ` Andrej Bauer
2009-06-12 12:56     ` Martin Jambon
2009-06-12 13:34     ` Martin Jambon
2009-06-12 15:43     ` Andreas Rossberg
2009-06-30 18:58       ` Yitzhak Mandelbaum
2009-06-30 20:19         ` Mike Lin
2009-06-30 22:06         ` Andreas Rossberg
2009-07-01  2:13           ` Mike Lin
2009-07-01  7:31             ` Andreas Rossberg
2009-07-01 14:02               ` Mike Lin
2009-07-01 14:17                 ` Andreas Rossberg
2009-07-01 14:21                   ` Andreas Rossberg
2009-07-01 14:37                     ` Mike Lin
2009-07-01 15:03                   ` Sylvain Le Gall
2009-07-01 15:16                     ` [Caml-list] " Andreas Rossberg
2009-07-01 16:26                       ` Sylvain Le Gall [this message]
2009-07-01 15:19                     ` Martin Jambon
2009-07-01 15:43                       ` Andreas Rossberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrnh4n3hj.hil.sylvain@gallu.homelinux.org \
    --to=sylvain@le-gall.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).