sedlex icon indicating copy to clipboard operation
sedlex copied to clipboard

Interop with `StdLib.Lexing` and compiler-libs' `Location`

Open clembu opened this issue 6 years ago • 1 comments

Ocaml's own compiler libraries provide cool ways to highlight source code and display syntax errors, through Location.print_report.

I managed to use it to display Menhir's messages with the location:

File "test_input", line 5, characters 9-10:
Error: Headers must not have trailing markers

But if I want it to have source highlighting like so:

File "inkc/lib/lexer.ml", line 13, characters 33-38:
13 | let any_blank = [%sedlex.regexp? blank | newline]
                                      ^^^^^
Error: Sedlex: unbound regexp blank

then I have to give it a Lexing.lexbuf instance with Location.input_buf := ...

It'd be nice if Sedlex had buffer conversion out of the box to support this.

As it is now, I can't even create a Lexing.lexbuf myself out of a Sedlexing.lexbuf because I don't have access to the buffer's actual contents. All I can get are locations. And small bits of the contents through the lexeme functions

clembu avatar Nov 09 '19 14:11 clembu

You should be able to get it work by setting Location.input_buf := None. Reading from an existing lexbuf seems to be an optimization only.

hhugo avatar Feb 25 '23 15:02 hhugo