| Newsgroups |
gmane.comp.lang.ocaml.beginners |
| Message-ID |
<[email protected]> |
Here is a minimal example showing my problem, it uses just one
silly_lexer.mll file, whose contents are :
{
let accu=ref[]
let add_to_accu x=(accu:=x::(!accu));;
}
rule silly_parse = parse
_ as c
{ add_to_accu (c,Lexing.lexeme_start_p lexbuf); silly_parse lexbuf}
| eof { List.rev(!accu) }
{
let parse_file fn=
let _=(accu:=[]) in
let in_channel=open_in fn in
let answer=silly_parse (Lexing.from_channel in_channel) in
close_in in_channel; answer
}
I execute ocamllex silly_lexer.mll, enter my toplevel and type
OCaml version 4.02.1
# #use"silly_lexer.ml";;
(...)
# let example=parse_file "silly_lexer.ml";;
val example : (char * Lexing.position) list =
[('#', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 0});
(' ', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 1});
('1', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 2});
(' ', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 3});
('"', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 4});
('s', {Lexing.pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 5});
Notice how the Lexing.pos_fname is set to "" rather "silly_lexer.ml".
Sure, I know how to manually set this Lexing.pos_fname field, but I can't help
feeling that this is not the clean way to do things. Why doesn't Ocaml
automatically set it for me when Lexing.from_channel is called ? What am I missing ?