Version française
Home     About     Download     Resources     Contact us    
Browse thread
ocamllex and python-style indentation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Sylvain Le Gall <sylvain@l...>
Subject: Re: ocamllex and python-style indentation
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