Version franēaise
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Lexing.lexeme_start_p broken?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] Lexing.lexeme_start_p broken?

Scott Duckworth writes:
 > I can't seem to get the function Lexing.lexeme_start_p to return a 
 > position with correct information in it.  Here is my code (test.mll):

In the Ocaml manual, in the documentation of the Lexing module, you
can read:

 "Note that the lexing engine will only manage the pos_cnum field of
  lex_curr_p by updating it with the number of characters read since
  the start of the lexbuf. For the other fields to be accurate, they
  must be initialised before the first use of the lexbuf, and updated
  by the lexer actions."

(below the "type lexbuf = ..."). To update these fields, the best way
is to look into ocaml sources to see how this is done is ocaml's own
parser. In file parsing/lexer.mll the function update_loc is doing the
job, being called each time a newline character is read. It is quite
complicated, because it handles many different things at the same
time, but to update the fields pos_lnum and pos_bol, it can be
simplified to 

  let update_loc lexbuf =
    let pos = lexbuf.lex_curr_p in
    lexbuf.lex_curr_p <- 
      { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum }

then you call this function for each newline in your lexer actions, e.g.

  | '\n' 
      { newline lexbuf; token lexbuf }

Hope this helps,
-- 
Jean-Christophe Filliātre (http://www.lri.fr/~filliatr)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners