Commit 2de38f5f authored by POTTIER Francois's avatar POTTIER Francois

New module MenhirLib.LexerUtil.

parent 38d5a3f3
open Lexing
open Printf
let init filename lexbuf =
lexbuf.lex_curr_p <- {
pos_fname = filename;
pos_lnum = 1;
pos_bol = 0;
pos_cnum = 0
let read filename =
let c = open_in filename in
let text = really_input_string c (in_channel_length c) in
close_in c;
let lexbuf = Lexing.from_string text in
text, init filename lexbuf
let newline 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;
let is_dummy (pos1, pos2) =
pos1 == dummy_pos || pos2 == dummy_pos
let range ((pos1, pos2) as range) =
if is_dummy range then
sprintf "At an unknown location:\n"
let file = pos1.pos_fname in
let line = pos1.pos_lnum in
let char1 = pos1.pos_cnum - pos1.pos_bol in
let char2 = pos2.pos_cnum - pos1.pos_bol in (* yes, [pos1.pos_bol] *)
sprintf "File \"%s\", line %d, characters %d-%d:\n"
file line char1 char2
(* use [char1 + 1] and [char2 + 1] if *not* using Caml mode *)
open Lexing
(* [init filename lexbuf] initializes the lexing buffer [lexbuf] so
that the positions that are subsequently read from it refer to the
file [filename]. It returns [lexbuf]. *)
val init: string -> lexbuf -> lexbuf
(* [read filename] reads the entire contents of the file [filename] and
returns a pair of this content (a string) and a lexing buffer that
has been initialized, based on this string. *)
val read: string -> string * lexbuf
(* [newline lexbuf] increments the line counter stored within [lexbuf]. It
should be invoked by the lexer itself every time a newline character is
consumed. This allows maintaining a current the line number in [lexbuf]. *)
val newline: lexbuf -> unit
(* [range (startpos, endpos)] prints a textual description of the range
delimited by the start and end positions [startpos] and [endpos].
This description is one line long and ends in a newline character.
This description mentions the file name, the line number, and a range
of characters on this line. The line number is correct only if [newline]
has been correctly used, as described dabove. *)
val range: position * position -> string
......@@ -7,6 +7,7 @@ IncrementalEngine
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment