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: Andreas Rossberg <rossberg@m...>
Subject: Re: [Caml-list] ocamllex and python-style indentation
Mike Lin wrote:
> OK, now I'm curious :) how does your lexer match balanced parentheses,
> or in this case comments?
>   

Easily, with a bit of side effects (I think that's roughly how all ML 
compilers do it):

------------------------------------------------
let error l s = (* ... *)
let commentDepth = ref 0
let start = ref 0
let loc length = let pos = !start in (pos, pos+length)

rule lex =
    parse eof            { EOF }
    (* | ... *)
    | "{-"            { start := pos lexbuf;
                  lexNestComment lexbuf }

and lexNestComment =
    parse eof            { error (loc 2) "unterminated comment" }
    | "(*"            { incr commentDepth;
                  lexNestComment lexbuf }
    | "*)"            { decr commentDepth;
                  if !commentDepth > 0
                  then lexNestComment lexbuf
                  else lex lexbuf }
    | _            { lexNestComment lexbuf }
------------------------------------------------

If you also want to treat strings in comments specially (like OCaml), 
then you need to do a bit more work, but it's basically the same idea.

- Andreas